What is the difference between PHP4 and php5?

Source: Internet
Author: User
Tags copy reference variable

The objects in PHP 5, which arenow the mainstream development language, have been systematically and comprehensively tuned, and may now look something like Java. This section focuses on the new object pattern in PHP(now the mainstream development language)5, with some simpler examples to illustrate. Make this section a new starting point for your 5 tour of PHP, which isnow the mainstream development language. :)

* 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 PHP(now the mainstream development language)4, when the function has the same name as the object, the function becomes the constructor of the object, and there is no concept of destructors in PHP(as the current mainstream development language)4.
In PHP(now the mainstream development language)5, constructors are uniformly named __construct, and the concept of destructors is introduced, which is uniformly named __destruct.

Example one: constructors and destructors

(as the current mainstream development language)
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 PHP(as the current mainstream development language)4, passing variables to a function or method, is actually a copy of this variable, it means that you pass to the function or method is a copy of this variable, unless you use the reference symbol "&" To declare is to make a reference, not a Copy. In PHP(as the current mainstream development language)5, objects are always in the form of references, and assignment operations in objects are also a reference operation.

Example two: A reference to an object


(as the current mainstream development language)
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

What if I want to get a copy of an object that is always invoked as a reference, as described above? PHP(as the current mainstream development language)5 provides a new feature, that is, the cloning of objects, syntax for __clone.

Example three: Cloning of objects
(as the current mainstream development language)
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 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 mode in the object

PHP(as the current mainstream development language)4, all the methods and variables of an object are public, which means that you can manipulate any one of these variables and methods outside of an object. PHP(as the current mainstream development language)5 introduces three new patterns for controlling this access: public, protected (Protected), and Private (privacy).)

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.

Example four: Private, public, and protected mode in the object

-->(as the current mainstream development language)
class Foo {
Private $x;
Public Function Public_foo () {
Print ("I ' m public");
}
protected function Protected_foo () {
$this->private_foo (); Ok because we are in the same class we can call private methods
Print ("I ' m protected");
}
Private Function Private_foo () {
$this->x = 3;
Print ("I ' m private");
}
}
Class Foo2 extends Foo {
Public Function display () {
$this->protected_foo ();
$this->public_foo ();
$this->private_foo (); invalid! The function is private in the base class
}
} $x = new Foo ();
$x->public_foo ();
$x->protected_foo (); Invalid cannot call protected methods outside the class and derived classes
$x->private_foo (); Invalid private methods can only used inside the class $x 2 = new Foo2 ();
$x 2->display ();
?>
Tip: Variables in an object are always in the private form, and directly manipulating variables in an object is not a good object-oriented programming habit, and a better approach is to give the variable you want to an object.



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.