PHP Oop
PHP Oop
Objects hide a lot of their inner workings away from the code that uses them,
providing instead easy interfaces through which you can send them orders and they
can return information. These interfaces are special functions called methods. All
the methods of an object have access to special variables called properties.
To create an object, you must first design the template from which it can be
instantiated. This template is known as a class, and in PHP, you must declare it
with the class keyword:
class Item {
// a very minimal class
}
$obj1 = new Item();
$obj2 = new Item();
print "\$obj1 is an ".gettype($obj1)."<br />";
print "\$obj2 is an ".gettype($obj2)."<br />";
Object Properties
Objects have access to special variables called properties. You can declare them
anywhere within the body of your class. A property can be a value, an array, or
even another object:
class Item {
var $name = "item";
}
Object Methods
A method is a function defined within a class. Every object instantiated from the
class has the method's functionality.
class Item {
var $name = "item";
function getName() {
return "item";
}
}
function getName () {
return $this->name;
}
}
$item = new Item ();
$item->name = "widget 5442";
print $item->getName ();
// outputs "widget 5442"
?>
A class uses the special variable $this to refer to the currently instantiated
object
Although you refer to an object by the handle you have assigned it to ($item, for
example), an object must refer to itself by means of the $this variable. Combining
the $this pseudovariable and ->, you can access any property or method in a class
from within the class itself.
function setName( $n ) {
$this->name = $n;
}
function getName() {
return $this->name;
}
}
class Item {
var $name;
$this->name = $name; }
$this->name = $n; }
function getName () {
return $this->name;
}
}
PHP 5 introduces a new syntax for constructor methods. Instead of using the name of
the class, you can use the special syntax __construct().
class item {
var $name;
var $code;
var $productString;
function getProductString () {
return $this->productString;
}
function setName( $n ) {
$this->name = $n;
$this->productString = $this->name." ".$this->code;
}
function getName () {
return $this->name;
}
}
$item->name = "widget-upgrade";
print $item->getProductString ();
Privacy Level
Description
public
private
private $name;
private $code;
private $productString;
Client coders are now forced to use the setName() method to change the $name
property.
function setName( $n ) {
$this->name = $n;
$this->makeProductString( $n, $this->code );
}
function makeProductString( $string, $code) {
return $this->productString = "$string $code";
}
Inheritance
To create a class that inherits functionality from a parent class, we need to alter
our class declaration slightly.
class Item {
var $name;
function getName() {
return $this->name;
}
}
function getName() {
return $this->name;
}
}
class Item {
private $name;
function getName() {
return $this->name;
}
}
function getName () {
return $this->name;
}
}
function getName() {
return "(price) ".parent::getName ();
}
}
if ( class_exists( $class_name ) ) {
$obj = new $class_name( );
}
method_exists() requires two arguments, an object and a string containing the name
of the method you are checking for:
class apple {
var $flavor="sweet";
}
$app = new apple();
$stored = serialize( $app );
print $stored;
You can convert the string produced by serialize() back into an object with the
unserialize() function. If the original class is present at the time unserialize()
is called, an exact copy of the original object is produced:
In some circumstances, you need your objects to clean up a little before storage.
This cleanup is particularly important if an object has a database connection open
or is working with a file. By the same token, you might want your object to perform
some sort of initialization when it is woken up. You can handle these needs by
including two special methods in any object that might need to be serialized.
class apple {
var $flavor="sweet";
var $frozen = 0;
function ___sleep( ) {
$this->frozen++;
// any clean up stuff goes here
return array_keys( get_object_vars( $this) );
}
}
$app = new apple ( );
$stored = serialize( $app );
print $stored;
function __wakeup( ) {
print "This apple has been frozen ".$this->frozen." time(s)";
// any initialization stuff goes here
}
Now that we have added __wakeup(), we can call unserialize();
$new_app = unserialize( $stored );
// prints "This apple has been frozen 1 time(s)"