Changes

Jump to: navigation, search

PHP Object Oriented Programming

3,053 bytes added, 20:25, 6 June 2007
Subclassing in PHP
?>
</pre>
 
== PHP Object Serialization ==
 
One of the interesting feature of object oriented programming is the ability to take a snapshot of the current state of an object and then save that object to a file, or even transmit it over a network to another process where it will be re-activated. This concept is known in the object oriented world as object serialization.
 
All objects have built-in mehtod called ''__sleep'' that is called before serialization. If you need your object to perform any housekeeping before being serialized yuou will need to override this method.
 
An object is serialized using the ''serialize()'' function and unserialized using the ''unserialize()'' function. As an example we can serialize our bankAccount object:
 
<pre>
<?php
class bankAccount {
 
private $accountNumber;
private $accountname;
 
public function __construct($acctNumber, $acctName) {
$this->accountNumber = $acctNumber;
$this->accountname = $acctName;
}
 
public function __destruct() {
}
 
public function setAccountNumber($acctNumber)
{
$this->accountNumber = $acctNumber;
}
 
public function setAccountName($acctName)
{
$this->accountName = $acctName;
}
 
public function getAccountName()
{
return $this->accountName;
}
 
public function getAccountNumber()
{
return $this->accountNumber;
}
 
}
 
$myObj = new bankAccount('246810', 'Morgan Freeman');
 
$serialized = serialize ($myObj);
 
echo 'Object is serialized<br>';
 
$newObj = unserialize ($serialized);
 
echo 'Object is unserialized<br>';
 
print_r ($newObj);
?>
</pre>
 
In teh above example the object is serialized, the unserialized to a new object. We then use the ''print_r()'' function to verify the new object contains everything the old one did resulting in the following output:
 
<tt>
Object is serialized<br>
Object is unserialized<br>
bankAccount Object ( [accountNumber:private] => 246810 [accountname:private] => Morgan Freeman )<br>
</tt>
 
Once we have the serialed data in our $serialized we can do anyt we wan twith 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 ==
 
There are number of ways to find out information about classes.
 
An array of classes to which your script has access can be obtained using the ''get_declared_classes()'' function. The ''class_exists()'' function can be passed the name of a class to find out if such a class exists. A list of methods in class can be obtained by passing the class name through the ''get_class_methods()'' function.
 
It is also possible to find out if a class has a parent class using the ''get_parent_class()'' function which will either return the name of the parent class, or an empty string if no parent exists.
 
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.

Navigation menu