Thursday, May 12, 2011

Patterns in PHP

more info.. http://evergreenphp.blogspot.com

Patterns
Patterns are ways to describe best practices and good designs. They show a flexible solution to
common programming problems.
Factory
The Factory pattern allows for the instantiation of objects at runtime. It is called a Factory Pattern since it is responsible for "manufacturing" an object. A Parameterized Factory receives the name of the class to instantiate as argument.

Singleton
The Singleton pattern applies to situations in which there needs to be a single instance of a class. The
most common example of this is a database connection. Implementing this pattern allows a
programmer to make this single instance easily accessible by many other objects.


Magic Methods

The function names __construct, __destruct, __call, __callStatic, __get, __set, __isset, __unset,
__sleep, __wakeup, __toString, __invoke, __set_state and __clone are magical in PHP classes. You
cannot have functions with these names in any of your classes unless you want the magic functionality
associated with them.
Caution
PHP reserves all function names starting with __ as magical. It is recommended that you do not use
function names with __ in PHP unless you want some documented magic functionality.
__sleepand __wakeup

serialize() checks if your class has a function with the magic name __sleep. If so, that function is
executed prior to any serialization. It can clean up the object and is supposed to return an array with the
names of all variables of that object that should be serialized. If the method doesn't return anything then
NULL is serialized and E_NOTICE is issued.
The intended use of __sleep is to commit pending data or perform similar cleanup tasks. Also, the
function is useful if you have very large objects which do not need to be saved completely.
Conversely, unserialize() checks for the presence of a function with the magic name __wakeup. If
present, this function can reconstruct any resources that the object may have.
The intended use of __wakeup is to reestablish any database connections that may have been lost
during serialization and perform other reinitialization tasks.

__toString
The__toStr ing method allows a class to decide how it will react when it is converted to a string.
It is worth noting that before PHP 5.2.0 the __toString method was only called when it was directly
combined with echo() or print(). Since PHP 5.2.0, it is called in any string context (e.g. inprintf() with
%s modifier) but not in other types contexts (e.g. with %d modifier). Since PHP 5.2.0, converting
objects without__toStr ing method to string would causeE_RECOVERABLE_ERROR.

__invoke
The__invoke method is called when a script tries to call an object as a function.
__set_state
Thiss tatic method is called for classes exported byvar_expor t() since PHP 5.1.0.
The only parameter of this method is an array containing exported properties in the form
array('property' => value, ...)

No comments: