Differences between php4 and php5

Source: Internet
Author: User

Objects in PHP5 have been adjusted systematically and comprehensively. The current situation may look similar to Java. This section describes the new object mode in PHP5 and provides some simple examples. Let this section be a new starting point for your PHP5 journey. :)

* Constructor and destructor

* Object Reference

* Object cloning

* Private, public, and protected modes in the object

* Interface (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 becomes the constructor of the object, and there is no destructor concept in PHP4.

In PHP5, constructor is uniformly named as _ construct, and the concept of destructor is introduced. It is uniformly named as _ 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 ");

}

}

$ O1 = new foo (4 );

$ O1-> display ();

?>

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

Object Reference

As we all know, in PHP4, when a variable is passed to a function or method, the variable is actually copied once, 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 as references, and assignment operations in objects are also a reference operation.

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! ");

?>

Object cloning

As mentioned above, when an object is always called as a reference, what should I do if I want a copy of the object? PHP5 provides a new function, namely object cloning, with the syntax of _ clone.

Example 3: Object cloning

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 object cloning method exists in many other application languages, so you don't have to worry about its stability. :)

Private, public, and protection modes of Objects

In PHP4, all methods and variables of an object are public, which means that you can operate any of the variables and methods in an object's external operations. PHP5 introduces three new access control modes: Public, Protected, and Private ).

Public mode: Allows operation control outside the object.

Private: only the methods in the object can be controlled.

Protected Mode (Protected): allows this object and its parent object to control its operations.


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.