Although PHP5 has not yet been officially released (the development version already provides the 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 features of PHP5. These three major 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 use PHP4 's expressive means, which is only to improve the readability of the article.
* The section described in the article may have some discrepancy with the final release version of PHP5
Before PHP5 is officially released, you can always download from http://snaps.php.net to the latest compiled version to experience the new features PHP5 brings to us in person.
The new object pattern
The objects in the PHP5 have been systematically and comprehensively tuned, and now may look somewhat like 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 modes in the object
* Interface (Interfaces)
* Abstract class
* __call
* __set and __get
* Static members
Constructors and destructors
In PHP4, when a function has the same name as an object, the function becomes the constructor for that object, and there is no concept of destructors in PHP4.
In PHP5, the constructors are uniformly named __construct, and the concept of destructors is introduced, and 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 example above, when you terminate the call to the Foo class, its destructor will be called, and the above example will output "Bye Bye".
References to Objects
It is well known that in PHP4, passing a variable to a function or method actually makes a copy of the variable, 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 in the form of references, and the assignment operation in an object is also a reference operation.
Example two: reference to an object
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
As mentioned above, when an object is always invoked as a reference, what if I want to get a copy of the object? PHP5 provides a new function, that is, the cloning of objects, the syntax is __clone.
Example three: Cloning of an object
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 is 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 an object
In PHP4, all methods and variables of an object are public, which means that you can manipulate any one of the variables and methods in the outer part of an object. PHP5 introduces three new modes for controlling this access, which are public, protected (Protected), and private (privately).
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.
in the object,!--? PHP class Foo {
private $x;
Public Function Public_foo () { br> print ("I ' m public");
}
Protected function Protected_foo () {
$this->private_foo ();//ok because we is 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->p Rivate_foo (); Invalid private methods can only be used inside the class $x 2 = new Foo2 ();
$x 2->display ();
?
Tip: Variables in objects are always private, and it is not a good object-oriented programming habit to manipulate variables directly in an object, and a better way to do this is to hand over the variables 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 like "class Foo extends parent" to control it. In PHP4 and PHP5, an object can inherit only once, and multiple inheritance is not supported. However, there is a new noun in PHP5: interface, interface is a special object that has no specific processing code, it just defines the name and parameters of some methods, then the object can easily use the ' implement ' keyword to integrate the required interface, and then add the specific execution code.
Example five: interface
Interface Displayable {
function display ();
}
interface Printable {
function Doprint ();
}
Class Foo implements displayable,printable {
function display () {
Code
} function Doprint () {
Code
}
}
?>
This is to improve the readability of the code and the popularity of a great help, through the above example can be seen, the object Foo contains the displayable and printable two interfaces, then we can clearly know that the object Foo will have a display () method and a print () method, you can easily manipulate the object without worrying about how the object's interior works, just to understand the interface part.
Abstract class
Abstract classes cannot be instantiated.
Abstract classes, like other classes, allow you to define variables and methods.
Abstract classes can also define an abstract method, and the methods of the abstract class will not be executed, although they may be executed in their derived classes.
Example VI: abstract class
Abstract class Foo {
protected $x;
Abstract function display ();
function SetX ($x) {
$this->x = $x;
}
}
Class Foo2 extends Foo {
function display () {
Code
}
}
?>
__call
The PHP5 object has a new dedicated method, __call (), which is used to monitor other methods in an object. If you try to invoke a method that does not exist in an object, the __call method will be called automatically.
Example VII: __call
class Foo {
function __call ($name, $arguments) {
Print ("Did Your Call me?") I ' m $name! ");
}
} $x = new Foo ();
$x->dostuff ();
$x->fancy_stuff ();
?>
This particular method can be used to implement an "overload (overloading)" Action so that you can examine your parameters and pass the arguments by calling a private method.
Example VIII: Using __call to achieve "overload" action
Class Magic {
function __call ($name, $arguments) {
if ($name = = ' Foo ') {
if (Is_int ($arguments [0])) $this->foo_for_int ($arguments [0]);
if (is_string ($arguments [0])) $this->foo_for_string ($arguments [0]);
}
} Private Function Foo_for_int ($x) {
Print ("Oh an int!");
} Private Function Foo_for_string ($x) {
Print ("Oh a string!");
}
} $x = new Magic ();
$x->foo (3);
$x->foo ("3");
?>
> __set and __get
This is a great method, and the __set and __get methods can be used to capture variables and methods that do not exist in an object.
Example IX: __set and __get
class Foo {
function __set ($name, $val) {
Print ("Hello, you tried-put $val in $name");
}
function __get ($name) {
Print ("Hey asked for $name");
}
}
$x = new Foo ();
$x->bar = 3;
Print ($x->winky_winky);
?>
type Indication
In PHP5, you can indicate in an object's method that its argument must be an instance of another object.
Example Ten: Type indication
class Foo {
Code ...
}
Class Bar {
Public Function Process_a_foo (foo $foo) {
Some Code
}
}
$b = new Bar ();
$f = new Foo ();
$b->process_a_foo ($f);
?>
As you can see, we can explicitly indicate the name of an object before the argument, and PHP5 will recognize that this parameter will be an object instance.
Static members
Static members and static methods are referred to as "Object method (class methods)" and "Object variable (class variables)" In the terminology of polygon object programming.
The object method is allowed to be called before an object is instantiated. Similarly, an "object variable" can be controlled independently of an object before it is instantiated (it does not need to be controlled by an object's method).
Example 11: Object methods and Object variables
Class Calculator {
static public $pi = 3.14151692;
static public function Add ($x, $y) {
return $x + $y;
}
}
$s = calculator:: $pi;
$result = Calculator::add (3,7);
Print ("$result");
?>
Exception handling
Exception handling is an ideal way to deal with program errors in Java and C + +, and we are delighted to see that PHP5 has joined this application. You can try using "try" and "catch" to control the error.
Example 12: Exception handling
class Foo {
function Divide ($x, $y) {
if ($y ==0) throw new Exception ("Cannot divide by zero");
return $x/$y;
}
}
$x = new Foo ();
try {
$x->divide (3,0);
} catch (Exception $e) {
echo $e->getmessage ();
echo "n
n ";
Some catastrophic measure here
}
?>
In the example above, we use "Try" to execute the statement in curly braces, and when an error occurs, the code gives the error to the "Catch" clause, and in the "catch" clause you need to indicate that you want to give the error to an object to handle, which makes the code structure look clearer. Because now we can give all the error information to an object to handle.
Custom error Handling
You can easily control the unexpected in your program with custom handling of the wrong code. You just need to derive your own error control class from the exception class, and in your own error control class you need to have a constructor and a GetMessage method, and here's an example.
Example 13: Custom error handling
Class Weirdproblem extends Exception {
Private $data;
function Weirdproblem ($data) {
Parent::exception ();
$this->data = $data;
}
function GetMessage () {
Return $this->data. "caused a weird exception!";
}
}
?>
Now we can use "throw new Weirdproblem ($foo)" to throw an error handle, and if the error occurs in the code block of "Try", PHP5 will automatically give the error to the "Catch" section for processing.
Name space
Namespaces are useful for grouping or grouping functions on a class. It can combine some related classes or functions to make it easier to call later.
Example 14: namespaces
Namespace Math {
Class Complex {
.... Code ...
function __construct () {
Print ("Hey");
}
}
} $m = new Math::complex ();
?>
Note that you need to use namespaces in situations where you might want to declare two or more names of objects to do different things, and then you can put them in separate namespaces (but the interface is the same).
Translator Note: This article from Phpbuilder, from the above text we are pleased to see the PHP5 in the new additions to some of the outstanding features. We can also see some Java and C + + shadow, now the PHP5 has not officially released, wait until the actual release that day, hoping to bring all the PHP enthusiasts more surprises. Friends who are more interested in this area can log in to the official PHP newsgroup to learn about the update. The newsgroup address is news://news.php.net and can also be accessed by logging in to the Web interface http://news.php.net.
http://www.bkjia.com/PHPjc/315102.html www.bkjia.com true http://www.bkjia.com/PHPjc/315102.html techarticle Although PHP5 has not yet been officially released (the development version already provides the download), we can now begin to experience the surprises that the new version will bring to us. In the following introduction, I ...