2. OO PHP
Object Oriented PHP
Advantages over functional programming
• Systematic procedure for designing application by extracting nouns and verbs
from specifications and using CRC (Classes, Responsibilities, Collaborators)
Cards, then formation of a UML diagram.
• Much, much easier to maintain and add or change functionality
• Elimination of repetitious code - DRY (Don’t repeat yourself)
• Enables use of design patterns – super efficient algorithms that have evolved for
accomplishing common OOP application functionality
4. Fundamental Concepts
• Inheritance - extends
override methods Object Oriented PHP for Beginners
http://www.killerphp.com/tutorials/object-oriented-php/
• Setting Access Modifiers
• public, private, protected
Use getter/setter methods and private instance
variables (attributes)
• Static Method or Attribute – belongs to the class,
not the object instance (e.g., counters)
• Scope resolution operator - error if cannot resolve
Paamayim Nekudotayim*
Referencing Parent Class – self::
parent:: works with parent static or instance
methods but not parent attributes, as well as
$this-> refers to instance
Constructors/Destructors – give a default value
__construct($var=0){}
Cloning Objects clone keyword and __clone()
* Error Message in Hebrew
method
paam = One
• Comparing Objects == vs. === ayim = Doubled
nekudot = Dot
ayim = Doubled
7. Design Patterns
• The Strategy Pattern defines a family
of algorithms, encapsulates each • The Iterator Pattern provides a way to
one, and makes them access the elements of an aggregate
interchangeable (polymorphism). object sequentially without exposing its
Strategy lets the algorithm vary underlying representation.
independently from the clients that • The Adaptor Pattern changes the
use it. interface of one or more classes
• The Template Method pattern allows • The Decorator Pattern attaches
a programmer to define the skeleton additional responsibilities to an object
of an algorithm in a superclass and dynamically. Decorators provide a
delegate specific steps to a subclass. flexible alternative to subclassing.
Subclasses can redefine certain steps • The Singleton Pattern ensures a class
of an algorithm without changing the only has one instance and provides a
algorithm's structure. global point of access to it.
10. Objects
User
Photograph
Comment
These will also serve as database tables. Usually when you have a database
table you have an object for that table.
• Database
• singleton pattern and allows for
adaptor pattern use if database format
changes, e.g., from MySQL to Oracle
Session
Additional Requirements
• Pagination
• Thumbnail generation
13. References
Freeman, Eric., Freeman, Elisabeth, Sierra, K., Bates, B. (2004). Head first
design patterns. Sebastpol, CA: O’Reilly Media, Inc.
Horstmann, C. (2010). Big Java 4th edition. (pp. 512-513). United States of
America: John Wiley & Sons, Inc.
Koffman, E. (2010). Data structures: abstraction and design using Java. (pp.
685-694). United States of America: John Wiley & Sons, Inc.
Lynda.com. (2009, March 25). PHP with MySQL beyond the basics.
Retrieved from http://www.lynda.com/tutorial/653