PHP (PHP training PHP Tutorial) 5 of the objects have been a more systematic, more comprehensive adjustment, and now may look like some Java. This section focuses on the new object patterns in PHP5, with some simpler examples to illustrate. Let this section be a new starting point for your PHP5 journey. :)
* Constructors and destructors
* References to Objects
* Cloning of objects
* Private, public, and protected modes in the object
* Interface (Interfaces)
* Abstract class
* __call
* __set and __get
* Static members
Constructors and destructors
In PHP4, when a function has the same name as an object, the function becomes the constructor for that object, and there is no concept of destructors in PHP4.
In PHP5, the constructors are uniformly named __construct, and the concept of destructors is introduced, and is uniformly named __destruct.
Example one: constructors and destructors
class Foo {
var $x;
function __construct ($x) {
$this->x = $x;
}
function display () {
Print ($this->x);
}
function __destruct () {
Print ("Bye Bye");
}
}
$o 1 = new Foo (4);
$o 1->display ();
?>
In the example above, when you terminate the call to the Foo class, its destructor will be called, and the above example will output "Bye Bye".
References to Objects
It is well known that in PHP4, passing a variable to a function or method actually makes a copy of the variable, which means that you pass a copy of the variable to the function or method, unless you use the reference symbol "&" to declare that you want to make a reference, not a copy. In PHP5, objects always exist in the form of references, and the assignment operation in an object is also a reference operation.
Example two: reference to an object
class Foo {
var $x;
function SetX ($x) {
$this->x = $x;
}
function GetX () {
return $this->x;
}
}
$o 1 = new Foo;
$o 1->setx (4);
$o 2 = $o 1;
$o 1->setx (5);
if ($o 1->getx () = = $o 2->getx ()) print ("Oh my god!");
?>
Cloning of objects
As mentioned above, when an object is always invoked as a reference, what if I want to get a copy of the object? PHP5 provides a new function, that is, the cloning of objects, the syntax is __clone.
Example three: Cloning of an object
class Foo {
var $x;
function SetX ($x) {
$this->x = $x;
}
function GetX () {
return $this->x;
}
}
$o 1 = new Foo;
$o 1->setx (4);
$o 2 = $o 1->__clone ();
$o 1->setx (5); if ($o 1->getx ()! = $o 2->getx ()) print ("Copies is Independant");
?>
The method of object cloning exists in many other application languages, so you don't have to worry about its stability. :)
Private, public, and protected modes in an object
In PHP4, all methods and variables of an object are public, which means that you can manipulate any one of the variables and methods in the outer part of an object. PHP5 introduces three new modes for controlling this access, which are public, protected (Protected), and private (privately).
Common mode (public): Allows manipulation control outside the object.
Private mode: Only the methods within this object are allowed to manipulate it.
Protected Mode (Protected): Allows this object and its parent object to manipulate it.
PHP4 and PHP5 differences