1st close contacts with PHP5 (1)

Source: Internet
Author: User
1st close contacts with PHP5 (1) Source: PHPBuilder.com
Original Author: Luis Argerich
Translation: erquan
Erquan note: I have not had time to experience PHP5 yet. I just translated an article about foreigners.
The following are all translated by erquan. 1st times of doing these things do not mislead everyone. Sorry for any inaccuracy.
You can't do this. if you do, you may have translated it. if you don't, you may have translated it. Otherwise, you may be too tired to mislead everyone .... :)
Please indicate the source of the article when posting the post. thank you :)


The official version of PHP5 has not yet been released, but we can learn and experience the new PHP features that the development version has brought to us.
This article will focus on the following three new features of PHP5:
* New Object Mode
* Structured Exception handling
* Namespace

Note the following before the official start:
* Some examples in this article are implemented using the PHP4 method to enhance the readability of the article.
* The new features described in this article may differ from those described in the official version. please refer to the official version.

* New Object Mode

The new object mode of PHP5 is greatly upgraded based on PHP4. you will look like JAVA :(.
The text below will give a brief introduction to it, with a small example to let you start to experience the new features of PHP5
Come on ~~ :)

* Constructor and Destructor
* Object reference
* Clone object
* Three object modes: 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 the class name is the constructor of the class by default, and there is no destructor concept in PHP4. (Erquan note: This is the same as JAVA)
But since PHP5, constructor is named _ construct in a unified way, and the destructor __destruct (Erquan note: This is the same as Delphi, it can be seen that PHP5 has absorbed 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 ");
}
}

$ O1 = new foo (4 );
$ O1-> display ();
?>

After running, you will see the output "bye", because the class calls the _ destruct () destructor at the end ~~

Object reference

As you know, in PHP4, when a variable is passed to a function or method, a copy is actually passed, unless you use an access code & to declare
You are making a variable reference. In PHP5, objects are always specified as references:
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 note: you will see "Oh my god! "Output)
Clone object

As shown above, what if I want to use copy instead of getting an object reference? Implement the following 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 object cloning method has been applied to 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 outside the object-because the methods and variables are public. Three modes are referenced in PHP5 to control
Control permissions on variables and methods: Public, Protected, and Private)

Public: Methods and variables can be accessed at any time
Private: it can only be accessed inside the class, and the subclass cannot.
Protected: it can only be accessed in the internal and subclass of the class.

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 idea of OOP. you should use other methods to implement the set/get function.


Interface

As you know, the syntax for implementing inheritance in PHP4 is "class foo extends parent ". Both PHP4 and PHP5 do not support multi-inheritance, that is, they can only inherit from one class. The "interface" in PHP5 is such a special class: it does not implement a specific method, but is used to define the name and element of the method, then, use keywords to reference them together and 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 reading and comprehension of the code is very helpful: When you read this class, you will know that foo contains interfaces displayable and printable, and must have print () (Erquan note: it should be the doprint () method and the display () method. You don't have to know how they are implemented internally to easily operate on them as long as you see the foo declaration.

Virtual class

A virtual class is a class that cannot be instantiated. it can define methods and variables like a superclass.
You can also define virtual methods in a virtual class, and this method cannot be implemented in this class, but must be implemented in its subclass.

Example 6: virtual class

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, when you try to use a non-existent variable or method in the calls class, __call () will be automatically called:
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 special method is used to implement "method overloading", because you rely on a private parameter to implement and check this parameter:
Exampe 8: _ call Implementation method overload

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 you access or set an undefined variable, the 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 );
?>

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.