PHP __call __autoload __clone __tostring __sleep_php Tutorial

Source: Internet
Author: User
Tags mysql tutorial php debug unique id
, __wakeup detailed

1, __call
__call ($method, $arg _array) when calling an undefined method is to call this adapting

PHP Tutorial 5 has a new dedicated method, __call (), which is used to monitor other objects in an object.

Method. If you try to invoke a method that does not exist in an object, the __call method will be called automatically.

Example VII: __call

class Foo {
function __call ($name, $arguments) {
Print ("Did Your Call me?") I ' m $name! ");
}
} $x = new Foo ();
$x->dostuff ();
$x->fancy_stuff ();
?>

This special method can be used to implement the "overload (overloading)" action, so that you can check

Check your parameters and pass the arguments by calling a private method.


2, __autoload
The __autoload function, which is called automatically when trying to use a class that has not yet been defined.

Look at the following example

Wrote a MSYQL class,

MySQL Tutorial. php

Class mysql{
Funciton __construct () {
............
}
}


Now I'm going to use the MySQL class on the index.php page.

function __authload ($class) {
Include_once ("path". $class. ". PHP ");
}

$mysql =new MySQL ();

?>
Include_once ("path/". $class. ". PHP ");

path/is the path where the class file resides

$class is the name of the class at the time of invocation.

The following. PHP is of course an extension

A class file may not feel much better if the class file is a lot of time,

Each class has to include, it is too troublesome, as long as each page before writing a __autoload ()

Can

By calling this function, the scripting engine has the last chance to load the required classes before PHP fails.

3, __construct, __destruct


Constructors and Destructors [__construct __destruct ()] Well, his role in class classes is

Initialize and destroy variables Let's take a look at the examples to

Class db

{

function __construct ()
{

$this->mconnid=mysql_connect ($this->dbhost, $this->dbuser, $this-

>DBPWD);//Establish a connection

mysql_select_db ($this->dbname, $this->mconnid); Select number

According to the library

mysql_query ("Set names ' GBK '");//Set Database tutorial coded to GBK

}

__destruct: destructor, disconnecting

function __destruct ()
{
Mysql_close ($this->mconnid); Here are a few more questions ...

}
}

At this time we do not need to consider the data connection and shutdown, as long as $aa = new db (); OK.

For more detailed information, please see:

Http://www.bkjia.com/phper/18/aa7fc14039d6f49b02c646638588be7f.htm

4, __clone

__clone Magic Method

We know that objects can be directly assigned, such as
$p 2 = $p 1; Here is an object with two references

Then I do:

$p 1->say ();
$p 2->say ();

Can be executed, and the effect is the same.
We also have a way to:
$p 3 = clone $p 1; Note that clone is the Clone keyword, and the difference here is that $P3 is a

A new object.

At the same time we add a method to the class:

function __clone ()
{
$this->name = "I am a copy"; Note: The $this here is the clone-generated object itself, not

is the current class
}

Then we execute:

$p 3->say ();

Print out

Name: I am a copy
Age:20

Here we understand that the __clone () method is the method that executes when the object is cloned, and its role is to

A copy of the new clone.
Perform actions such as property initialization.

5, __tostring

The __tostring method automatically calls when an object is converted to a string

If I have a class:

Class Person
{
Private $name = "";
Private $age = 0;

function __construct ($name = "", $age = "")
{
$this->name = $name;
$this->age = $age;
}

function Say ()
{
echo "Name:" $this->name. "
”.” Age: ". $this->age."
”;
}
}

Now I'm going to instantiate this class and then print this example:

$p 1 = new Person ("Liuzy", 20);
echo $p 1; Direct printing will make an error

It is obvious that this is an error to print directly to the object, because the object is a reference handle and cannot be printed directly. This

, we can use the __tostring () method. We add a __tostring () method to the Person class

:
function __tostring ()
{
Return "I am person,my name is". $this->name. "
”;
}

Then refresh the page and find out what?
Now we understand that __tostring () is the method that executes when the object is printed directly, and we can use this method

Print some information about the class. Note: There are two underscores, the method must have a return value


6, __sleep, __wakeup
__sleep serialization is used when
__wakeup Crossdress is called when the line is being serialized

When PHP is serialized, serialize () checks if there is a __sleep () in the class, and if so, the letter

The number will run before any serialization. The function must return a member property that needs to be serialized for saving

Array, and only those member properties returned by the function are serialized. The function has two functions: first. In order

Before you do this, close any database connections that the object may have, and so on. Second. Specifies that the object needs to be serialized

If a property is larger and does not need to be stored, you can not write it into the __sleep to

Returns the array so that the property is not serialized

Conversely, after Unserialize () has created an object from the byte stream, it immediately checks to see if it has

The existence of a __wakeup function. If present, __wakeup is called immediately. Use of __wakeup

is to rebuild any database connections that might be lost in serialization and to handle other reinitialization tasks.

Class User
{
Public $name;
public $id;

function __construct ()
{
$this->id = Uniqid (); Give User a unique ID assigns a

A different ID.
}

function __sleep ()
{
Return (Array ("name")); Do not serialize This->id no

Serialization ID
}

function __wakeup ()
{
$this->id = Uniqid (); Give User a unique ID
}
}

$u = new User;
$u->name = "haha";

$s = serialize ($u); Serialize it serialization is not a bunch of attention

The row id attribute, the value of the ID is discarded

$u 2 = unserialize ($s); Unserialize it crossdress the line ID is

Re-assign Value


$u and $u 2 have different IDs for different IDs $u and $U2
Var_dump ($u);
Var_dump ($u 2);
?>

----------PHP Debug----------
Object (User) #1 (2) {
["Name"]=>
String (4) "Haha"
["id"]=>
String ("47fa045529f69")
}
Object (User) #2 (2) {
["Name"]=>
String (4) "Haha"
["id"]=>
String ("47fa04552a49a")
}


http://www.bkjia.com/PHPjc/445367.html www.bkjia.com true http://www.bkjia.com/PHPjc/445367.html techarticle , __wakeup 1, __call __call ($method, $arg _array) when calling an undefined method is to call this adapting PHP Tutorial 5 object added a special method __call (), this method ...

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