Home > Article > Backend Development > The first intimate contact with PHP5(1)_PHP tutorial
Article source: PHPBuilder.com
Original author: Luis Argerich
Translation: erquan
erquan Note: I haven’t had time to experience PHP5 yet, so I just translated an article written by a foreigner.
The following are all translated by erquan. I hope this is the first time I have done this and it has not misled anyone. Please understand some inaccuracies.
Let’s see if this works. If it works, I’ll finish the translation. If it doesn’t, I’ll just translate it, so as not to mislead everyone, which is also tiring. . . . :)
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 the following 3 new PHP5 features:
* New Object Mode
* Structured Exception Handling
* Namespace
Before officially starting, please note:
*Some examples in the article are implemented using PHP4, just to enhance the readability of the article
*The new features described in this article may be different from the features of the official version, please refer to the official version.
* New Object Mode
The new object mode of PHP5 has been greatly "upgraded" based on PHP4. You will look a lot like JAVA: (.
Some of the following text will do it Some brief introductions, with small examples to help you start experiencing the new features of PHP5
come on~~:)
* Constructors and destructors
* Object references
* Clone object
* 3 modes of objects: private, public and protected
* Interface
* Virtual class
* __call()
* __set() and __get()
* Static members
Constructor and destructor
In PHP4, a function with the same name as a class is defaulted to the constructor of the class, and there is no concept of a destructor in PHP4. (Erquan's note: This is the same as JAVA)
But starting from PHP5, the constructor is uniformly named __construct, and there is a destructor: __destruct (Erquan's note: This is the same as Delphi. It can be seen that PHP5 Congratulations for absorbing many mature OO ideas~~):
Example 1: Constructor and destructor
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 "bye bye" output. This is because the class terminates The __destruct() destructor was called~~
Object reference
As you know, in PHP4, when passing a variable to a function or method, a copy is actually passed, unless you use the address operator & to declare
You are making a reference to a variable. In PHP5, objects are always specified by reference:
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!");
?>
( Erquan's note: You will see the output of "Oh my god!")
Clone object
is as above. What should I do if sometimes I don’t want to get a reference to the object and want to use copy? Implemented in the __clone method provided by PHP5:
Example 3: Clone object
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 used in many languages, so you don’t have to worry about its performance :).
Private, Public and Protected
In PHP4, you can operate any of its methods and variables from outside the object - because the methods and variables are public.Three modes are cited in PHP5 to control
control permissions on variables and methods: Public (public), Protected (protected) and Private (private)
Public: methods and variables can be used anywhere When accessed
Private: can only be accessed inside the class, nor can it be accessed by subclasses
Protected: can only be accessed inside the class or subclasses
Example 4: Public, protected and private
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. Directly accessing a private variable is not a good OOP idea. You should use other methods to implement set/get functions
Interface
As you know, the syntax for implementing inheritance in PHP4 is "class foo extends parent". No matter in PHP4 or PHP5, multiple inheritance is not supported, that is, you can only inherit from one class. The "interface" in PHP5 is a special class: it does not specifically implement a method, but is used to define the name of the method and the elements it owns, and then reference them together through keywords to implement specific actions.
Example 5: Interface
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 know that foo contains The interfaces displayable and printable are provided, and there must be print() (Erquan's note: it should be doprint()) method and display() method. You can easily manipulate them without knowing how they are implemented internally 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 super class.
Virtual methods can also be defined in virtual classes, and this method cannot be implemented in this class, but must be implemented in its subclasses
Example 6: Virtual class
abstract class foo {
protected $x;
abstract function display();
function setX($x) {
$ this->x = $x;
}
}
class foo2 extends foo {
function display() {
}
}
?>
__call() method
In PHP5, if you define the __call() method, __call() will be automatically called when you try to access a non-existent variable or method in the class:
Example 7: __call
class foo {
function __call($name,$arguments) {
print("Did you call me ? I'm $name!");
}
}
$x = new foo();
$x->doStuff();
$x- >fancy_stuff();
?>
This particular method is customarily used to implement "method overloading" because you rely on a private parameter to implement and check this parameter:
Exampe 8: __call implements method overloading
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
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);
?>