Changes

PHP Object Oriented Programming

40 bytes added, 18:03, 13 May 2009
no edit summary
Class members are essentially variables and methods embedded into the class. Members can be ''public'' or ''private'' and ''static'' or ''variable''.
''public'' members can be accessed from outside the object. ''private'' members can only be accessed by methods contained in the class. This is the key to what is called ''data encapsulation''. Object-oriented programming convention dictates that data should be encapsulated in the class and accessed and set only through the methods of the class (typically called ''getters'' and ''setters'').
Members declared as ''static'' are immutable, in that once defined they cannot be changed (much like constants). Members and functions are prefixed with ''public'', ''private'' and ''static'' when declared in the class. The default is ''public'' ''non-static''.
== Subclassing in PHP ==
Once a class has been defined it is possible to create a new class derived from it that extends the functionality of the original class. The parent class is called the ''superclass'' and the child the ''subclass''. The whole process is referred to as ''subclassing''.
A subclass of a class can be defined using the ''extends'' keyword when declaring the subclass. For example we might choose to create a subclass of of our ''bankAccount'' class called ''savingsAccount'' which defines an interest bearing type of account:
<pre>
All objects have built-in method called ''__sleep'' that is called before serialization. If you need your object to perform any housekeeping before being serialized you will need to override this method.
An object is serialized using the ''serialize()'' function and unserialized, not surprizsinglysurprisingly, using the ''unserialize()'' function. As an example we can serialize our bankAccount object:
<pre>
</tt>
Once we have the serialized data in our $serialized object we can do anything we want with it, such as write it to a file or send it through a network socket to another process where it can be unserialized and used.
== Getting Information about a PHP Object ==
The ''method_exists()'' function, when passed an object pointer and method name as arguments, will return a true or false value indicating the existence of the method.
 
<google>BUY_PHP_BOTTOM</google>