Source of the article:
Original author: Luis Argerich
Translation: erquan
erquan note: I still haven't had time to experience PHP5, and I'm just translating a foreigner's article.
The following are translated by erquan. I hope that this is not misleading everyone for the first time. Please apologize for some inaccuracies.
Everyone thinks this is OK. If it works, I will finish the translation. If it doesn’t work, I will translate it, so as not to mislead everyone and be tired. . . . :)
Please indicate the source of the article when reposting, thank you:)
The official version of PHP5 has not been released yet, but we can learn and experience the new PHP features brought to us by the development version.
This article will focus on introducing the following three new PHP5 features:
* New object mode
* Structured exception handling
* Namespace
Before officially starting, please note:
*Some examples in the article are implemented using PHP4 method, just to enhance the readability of the article
*The new features described in this article may differ from the official version. Please refer to the official version.
* New object mode
PHP5's new object mode has made a big "upgrade" based on PHP4, and you will look a lot like JAVA:(.
The following text will give you some brief introduction to it, and there are small examples to start experiencing the new features of PHP5
come on~~:)
* Constructor and destructor
* Object reference
*Clone the object
* 3 modes of objects: private, public and protected
* Interface
* Virtual Class
* __call()
* __set() and __get()
* Static member
Constructors and destructors
In PHP4, functions with the same class name are defaulted to the constructor of the class, and there is no concept of a destructor in PHP4. (Erquan Note: This is the same as JAVA)
However, starting from PHP5, the constructor was uniformly named __construct, and there is a destructor: __destruct (Erquan Note: This is the same as Delphi. It can be seen that PHP5 has absorbed many mature OO ideas, which can be congratulated~~):
Example 1: Constructor and destructor
<?php
class foo {
var $x;
function __construct($x) {
$this->x = $x;
}
function display() {
print($this->x);
}
function __destruct() {
print("bye bye");
}
}
$o1 = new foo(4);
$o1->display();
?>
After running, you will see the output of "bye bye", because the class calls the __destruct() destructor when it terminates~~
References to objects
As you know, in PHP4, when passing a variable to a function or method, you actually pass a copy unless you use the address pass character & to declare it
You are making a reference to a variable. In PHP5, objects are always specified in reference form:
Example 2: Object reference
<?php
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!");
?>
(Erquan Note: You will see the output of "Oh my god!")
Cloning an object
As mentioned above, what should I do if I sometimes want to use copy without getting a reference to an object? Implemented in the __clone method provided by PHP5:
Example 3: Cloning an object
<?php
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 method of cloning objects has been applied to many languages, so you don't have to worry about its performance :).
Private, Public and Protected
In PHP4, you can manipulate its arbitrary methods and variables outside the object--because methods and variables are common. 3 modes are referenced to control in PHP5
Control permissions on variables and methods: Public (public), Protected (protected) and Private (private)
Public: Methods and variables can be accessed at any time
Private: Only accessible inside the class, and subclasses cannot
Protected: Only accessible inside or subclass of the class
Example 4: Public, protected and private
<?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 be used inside the class
$x2 = new foo2();
$x2->display();
?>
Tip: Variables are always private. Direct access to a private variable is not a good OOP idea. Other methods should be used to implement the function of set/get
interface
As you know, the syntax to implement inheritance in PHP4 is "class foo extends parent". Whether in PHP4 or PHP5, multiple inheritance is not supported, that is, it can only inherit from one class. The "interface" in PHP5 is a special class: it does not implement a certain method in detail, but is just used to define the name of the method and the elements it owns, and then reference them together through keywords and implement specific actions.
Example 5: Interface
<?php
interface displayable {
function display();
}
interface printable {
function doprint();
}
class foo implements displayable,printable {
function display() {
// code
}
function doprint() {
// code
}
}
?>
This is very helpful for the reading and understanding of the code: when you read this class, you will know that foo contains the interface displayable and printable, and there must be a print() (Erquan Note: it should be the doprint()) method and display() method. You don't have to know how they are implemented internally to easily operate them as long as you see the declaration of foo.
Virtual Class
A virtual class is a class that cannot be instantiated. It can define methods and variables like a superclass.
Virtual methods can also be defined in virtual classes, and the method cannot be implemented in the class, but must be implemented in its subclass.
Example 6: Virtual Class
<?php
abstract class foo {
protected $x;
abstract function display();
function setX($x) {
$this->x = $x;
}
}
class foo2 extends foo {
function display() {
// Code
}
}
?>
__call() method
In PHP5, if you define the __call() method, __call() will be automatically called when you try to access a variable or method that does not exist in the class:
Example 7: __call
<?php
class foo {
function __call($name,$arguments) {
print("Did you call me? I'm $name!");
}
}
$x = new foo();
$x->doStuff();
$x->fancy_stuff();
?>
This special method is used to implement "method overloading" because you rely on a private parameter to implement and check this parameter:
Example 8: pcall implementation method reload
<?php
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() method and __get() method
When accessing or setting an undefined variable, these two methods will be called:
Example 9: __set and __get
<?php
class foo {
function __set($name,$val) {
print("Hello, you tried to put $val in $name");
}
function __get($name) {
print("Hey you asked for $name");
}
}
$x = new foo();
$x->bar = 3;
print($x->winky_winky);
?>