1th Time Intimate Contact PHP5 (1) _php tutorial

Source: Internet
Author: User
Article Source: phpbuilder.com
Original Author: Luis Argerich
Translation: Erquan
Erquan Note: I have not had time to experience the PHP5, just translated a foreigner's article.
The following are translated by Erquan, the 1th time to do these things hope not to mislead everyone. Please understand some places that are not allowed.
Everyone see this line can not, if the line, I translated, not translated, so as not to mislead everyone, also tired oh .... :)
Please indicate the source of the article, 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 brings to us.
This article will focus on the following 3 big PHP5 new features:
* New Object mode
* Structured Exception handling
* Name space

Before you begin, please note that:
* Some examples in this article are implemented in PHP4, just to enhance the readability of the article
* The new features described in this article may differ from the official version, subject to the official edition.

* New Object mode

PHP5 the new object pattern has made a great "upgrade" on the basis of PHP4, and you will look like Java: (.
Some of the following text will give a brief introduction to it, with a small example to begin to experience the new features of PHP5
Come on~~:)

* Constructors and destructors
* References to Objects
* Cloning objects
* 3 Modes of the object: private, public, and protected
* Interface
* Virtual class
* __call ()
* __set () and __get ()
* Static members

Constructors and destructors

In PHP4, a function like the class name is defaulted to the constructor of that class, and there is no concept of destructors in PHP4. (Two springs Note: This is the same as Java)
But starting with PHP5, the constructor is named __construct, and has a destructor: __destruct (two springs Note: This is the same as Delphi, it can be seen PHP5 absorbed a lot of mature oo thought, C can congratulate ~ ~):
Example 1: 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 ();
?>

After running you will see the output "Bye Bye" because the class called The __destruct () destructor at the time of termination ~ ~

References to Objects

As you know, in PHP4, when you pass a variable to a function or method, you actually pass a copy, unless you use the address & to declare
You are making a reference to a variable. In PHP5, objects are always specified in a reference way:
Example 2: 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!");
?>

(Two springs Note: you will see "Oh my god!" 's output)
Cloning objects

As above, what if you sometimes want to use copy when you don't want to get a reference to the object? Implemented in the __clone method provided by PHP5:
Example 3: Cloning 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 cloning an object 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 arbitrary methods and variables on the outside of an object-because methods and variables are common. 3 modes are referenced in PHP5 to control
Control permissions on variables, methods: public (Common), Protected (protected) and private (private)

Public: Methods and variables can be accessed at any time
Private: can only be accessed within the class, and subclasses cannot access
Protected: Accessible only within the class, within 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 at 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

$x 2 = new Foo2 ();
$x 2->display ();
?>


Tip: Variables are always private, and accessing a private variable directly is not a good oop idea, and there should be other ways to implement the Set/get function.


Interface

As you know, the syntax for implementing inheritance in PHP4 is "class Foo extends parent". In both PHP4 and PHP5, multiple inheritance is not supported, which can only be inherited from one class. The "interface" in PHP5 is a special class: it does not implement a method specifically, it simply defines the name and the elements of the method, and then uses the keyword to refer to them together and implement the specific action.

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 reading and understanding code: When you read this class, you know that Foo contains interfaces displayable and printable, and there must be print () (Two springs Note: it should be the Doprint ()) method and the display () method. It is not necessary to know how they are implemented inside to easily manipulate them as soon as you see Foo's declaration.

Virtual classes

A virtual class is a class that cannot be instantiated, and it can define methods and variables like a superclass.
A virtual method can also be defined in a virtual class, and the method cannot be implemented in the 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 () {
Code
}
}
?>


__call () method

In PHP5, if you define the __call () method, __call () is automatically called when you try to access a nonexistent variable or method in the class:
Example 7: __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 is used to implement "method overloading" because you rely on a private parameter to implement and examine this parameter:
Exampe 8: __call Implementation 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 you access or set an undefined variable, these two methods are called:

Example 9: __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);
?>

http://www.bkjia.com/PHPjc/314174.html www.bkjia.com true http://www.bkjia.com/PHPjc/314174.html techarticle Article Source: phpbuilder.com Original Author: Luis Argerich translation: Erquan Erquan Note: I have not had time to experience PHP5, but only to translate an article of the foreigner. The following are translated by Erquan ...

  • 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.