Preliminary study on PHP5 (i.)

Source: Internet
Author: User
Tags copy exception handling inheritance interface new features variables reference variable
PHP5 Although PHP5 has not been officially released (the development version has already been available for download), we can now begin to experience the surprises that the new version will bring to us. In the following introduction, we will focus on the three major features of PHP5. The three main features are:

* New Object mode
* Exception Handling (Exceptions)
* Namespace (Namespace)

Before you begin, declare two points:

* Examples in the article to illustrate how to operate, some parts of the use of PHP4 performance means, this is only to improve the readability of the article.
* There may be some discrepancies between the sections described in the article and the final release of PHP5

Before the PHP5 is officially released, you can download it from http://snaps.php.net to the latest compiled version to personally experience the new features that PHP5 brings to us.


The new object pattern

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

<?php
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.

Example two: A reference to an object


<?php
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? PHP5 provides a new feature, that is, the cloning of objects, the syntax is __clone.

Example three: Cloning of objects
<?php
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

In PHP4, 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. PHP5 introduced three new patterns for controlling this access, which are public, protected (Protected), and proprietary (private).

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

<?php
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.


Interface (interfaces)

As we all know, objects in PHP4 support inheritance, and to make an object a derived class of another object, you need to use code similar to "class Foo extends parent" to control it. In PHP4 and PHP5, an object can only be inherited once, and multiple inheritance is not supported. However, a new noun was produced in the PHP5: interface, an interface is a special object that does not deal with code specifically, it only defines the name and parameters of some methods, then the object can easily use the ' implement ' keyword to integrate the required interfaces, and then add the specific execution code.

Example five: interface

<?php
Interface Displayable {
function display ();
}
interface Printable {
function Doprint ();
}

Class Foo implements displayable,printable {
function display () {
Code
function Doprint () {
Code
}
}
?>
This helps to improve the readability and popularity of the code, and as you can see from the example above, Object Foo contains the displayable and printable two interfaces, and we can clearly see that object Foo must have a display () method and a print () method, you simply need to understand the interface, and you can easily manipulate the object without having to worry about how the object's interior works.

To be Continued ~ ~



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.