The difference between PHP4 and PHP5

Source: Internet
Author: User
Tags copy reference

The objects in the PHP5 have been systematically and comprehensively tuned, and may now look somewhat similar to 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 mode in the object

* Interface (interfaces)

* Abstract class

* __call

* __set and __get

* Static member

Constructors and destructors

In PHP4, when a function has the same name as an object, the function becomes the constructor of the object, and there is no concept of destructors in the PHP4.

In PHP5, constructors are uniformly named __construct, and the concept of destructors is introduced, which 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 above example, when you terminate the call to the Foo class, its destructor will be invoked, and the "Bye Bye" will be printed in the example above.

References to Objects

As we all know, in the PHP4, passing a variable to a function or method, which actually makes a copy of the variable, means that you pass a copy of the variable to the function or method, unless you use the reference symbol "&" to declare a reference, not a copy. In PHP5, an object is always in the form of a reference, and the assignment operation in the object is also a reference operation.

1 2 Next page > full text reading tips: Try "←→" button, turn the page more convenient Oh!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.