1th Time Intimate Contact PHP5 (1) _php Foundation

Source: Internet
Author: User
Tags abstract exception handling

Article Source: phpbuilder.com
Original Author: Luis Argerich
Translation: Erquan
Erquan Note: I have not had time to experience PHP5, just translation of a foreigner article.
The following are translated by Erquan, and the 1th time we do these things hopefully not to mislead you. Please forgive me for some of the forbidden places.
We see such a line is not, if the line, I translated, no translation, lest mislead everyone, also tired oh .... :)
Please specify the source of the article, thank you:


The official version of PHP5 has not been released yet, 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 major PHP5 new features:
* New Object mode
* Structured Exception handling
* Name space

Before the formal start, please note:
* Some examples in this article are implemented by PHP4 method, just to enhance the readability of the article
* The new features described in this article may differ from the formal version, please refer to the official version.

* New Object mode

PHP5 the new object pattern is a big "upgrade" based on PHP4, and you will look much like Java: (.
Some of the following text will give a brief introduction to it, with a small example that lets you start experiencing the new features of PHP5
Come on~~:)

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

Constructors and destructors

In PHP4, the same function as the class name is default to the constructor of the class, and there is no concept of destructors in PHP4. (Second fountain Note: This is the same as Java)
However, starting from PHP5, the constructors are uniformly named __construct and have destructors: __destruct (two springs Note: This is the same as the Delphi, visible PHP5 absorbed a large number of mature oo thought, can be C ~ ~):
Example 1: Constructors and destructors

<?php
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 ();
?>

You will see the output of the "Bye Bye" after you run it, because the class called The __destruct () destructor at the end of the function ~ ~

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 character & 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

<?php
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!") The output)
Cloning objects

What if, as above, you 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

<?php
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 are 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 any of its methods and variables outside of the object-because the methods and variables are public. In PHP5, 3 models are cited 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 a class, and subclasses cannot access
Protected: can only be accessed within the class, in subclasses

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

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


Tip: Variables are always private, direct access to a private variable is not a good oop idea, should use other methods to realize the function of Set/get


Interface

As you know, the syntax for implementing inheritance in PHP4 is "class Foo extends parent." Multiple inheritance is not supported in both PHP4 and PHP5, and can only be inherited from one class. The "interface" in PHP5 is a special kind of class: it does not implement a method specifically, it is used to define the name of the method and the elements it owns, and then reference them together and implement specific actions by keyword.

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 readability and understanding of the code: when you read the class, you know that Foo contains the interface displayable and printable, and there must be a print () (Second spring Note: the Doprint ()) method and the display () method. You don't have to know how to implement them internally so you can easily manipulate them as soon as you see Foo's statement.

Virtual class

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 it cannot be implemented in the class, but must be implemented in its subclasses

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 () is automatically invoked when you attempt 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 particular 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

<?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 you access or set an undefined variable, the two methods are invoked:

Example 9: __set and __get

<?php
class Foo {

function __set ($name, $val) {
Print ("Hello, tried to put $val in $name");
}

function __get ($name) {
Print ("Hey 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.