Home > Backend Development > PHP Tutorial > The difference between php4 and php5_PHP tutorial

The difference between php4 and php5_PHP tutorial

WBOY
Release: 2016-07-15 13:22:29
Original
915 people have browsed it

The objects in PHP5 have been adjusted more systematically and comprehensively, and their current appearance may look somewhat similar to Java. This section focuses on the new object mode in PHP5 and gives some simpler examples to illustrate. Let this section be a new starting point for your PHP5 journey. :)

* Constructor and destructor

* Object reference

* Object clone

* Private, public and receiver in the object Protected Mode

* Interfaces

* Abstract Class

* __call

* __set and __get

* Static members

Constructor and destructor

In PHP4, when a function has the same name as an object, this function will become the constructor of the object, and there is no such function in PHP4 The concept of destructor.

In PHP5, the constructor is uniformly named __construct, and the concept of destructor is introduced, which is uniformly named __destruct.

Example 1: Constructor and destructor

class foo {

var $x;

function __construct($x) {

$this->x = $x;

}

function display() {

print($this->x);

}

function __destruct() {

print("bye bye");

}

}

$ o1 = new foo(4);

$o1->display();

?>

In the above example, when you terminate the call to foo class , its destructor will be called, and "bye bye" will be output in the above example.

Object reference

As we all know, in PHP4, passing a variable to a function or method actually copies the variable, which means that the variable you pass to the function or method is copied. Is a copy of this variable, 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 assignment operations in objects are also reference operations.

Example 2: Object reference

class foo {

var $x;

function setX($x) {

$this->x = $x;

}

function getX() {

return $this->x;

}

}

$o1 = new foo;

$o1->setX(4);

$o2 = $o1;

$o1->setX(5);

if($o1->getX() == $o2->getX()) print("Oh my god!");

?>

Clone of object

As mentioned above, when an object is always called as a reference, if I want to get the What should I do if I have a copy of the object? PHP5 provides a new feature, which is object cloning, with the syntax __clone.

Example 3: Cloning of objects

class foo {

var $x;

function setX($x) {

$this->x = $x;

}

function getX() {

return $this->x;

}

}

$o1 = new foo;

$o1->setX(4);

$o2 = $o1->__clone( );

$o1->setX(5); if($o1->getX() != $o2->getX()) print ("Copies are 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 objects

In PHP4, all methods and variables of an object are public, which means you can operate on it outside of an object any variable and method. PHP5 introduces three new modes for controlling this access permission: Public, Protected, and Private.

Public mode (Public): allows operation control outside the object.

Private mode (Private): Only methods within this object are allowed to operate and control it.

Protected mode (Protected): Allows this object and its parent object to operate and control it.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446963.htmlTechArticleThe objects in PHP5 have been adjusted more systematically and comprehensively, and they may look similar now. in Java. This section focuses on the new object mode in PHP5 and gives an example...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template