Php _ call _ autoload _ clone _ toString _ sleep_PHP tutorial

Source: Internet
Author: User
Tags php debug
Php _ call _ autoload _ clone _ toString _ sleep. , _ Wakeup explanation 1, _ call ($ method, $ arg_array) when an undefined method is called, a special method _ call () is added to the object in php Tutorial 5. this method and _ wakeup are described in detail.

1. _ call
_ Call ($ method, $ arg_array) when an undefined method is called

A special method _ call () is added to the object in php Tutorial 5, which is used to monitor other

Method. If you try to call a method that does not exist in an object, the __call method 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 can be used to implement the "overload" action, so that you can check

Query your parameters and pass the parameters by calling a private method.


2. _ autoload
_ Autoload function, which is automatically called when trying to use a class that has not been defined.

See the following example.

Write a msyql class,

Mysql tutorial. php

Class mysql {
Funciton _ construct (){
............
}
}


Now I want 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 of the class file

$ Class is the name of the class used for calling.

Of course, the extension of. php is later,

A class file may not feel much useful. if there are many class files,

Every class needs to be included, which is too troublesome, as long as a _ autoload ()

Yes,

By calling this function, the script engine has the last chance to load the required class before a php error fails.

3. _ construct and _ destruct


Constructor and Destructor [_ construct _ destruct ()] Oh, his role in class is

The initialization and destruction variables let's take a look at the instance

Class db

{

Function _ construct ()
{

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

> Dbpwd); // establish a connection

Mysql_select_db ($ this-> dbname, $ this-> mconnid); // Number of selections

Database

Mysql_query ("set names 'gbk'"); // sets the database tutorial encoding to gbk.

}

// _ Destruct: destructor, disconnected

Function _ destruct ()
{
Mysql_close ($ this-> mconnid); // There is a problem ......

}
}

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

For more details, see:

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

4. _ clone

_ Clone magic method

We know that objects can be directly assigned values, for example
$ P2 = $ p1; // here an object has two references

Then execute:

$ P1-> say ();
$ P2-> say ();

Yes, and the effect is the same.
We have another method:
$ P3 = clone $ p1; // note that clone is a clone keyword. The difference here is that $ p3 is

New object.

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

Function _ clone ()
{
$ This-> name = "I am a Copy"; // Note: Here $ this is the cloned object itself, not

Is the current class
}

Then we execute:

$ P3-> say ();

Print out

:

Name: I am a copy
Age: 20

Here we understand that the __clone () method is executed when the object is cloned. its function is

New cloned copy
Initialize attributes.

5. _ tostring

The _ tostring method is automatically called 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 will instantiate this class and print this instance:

$ P1 = new person ("liuzy", 20 );
Echo $ p1; // error when printing directly

Obviously, it is wrong to print the object directly 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 something?
Now we understand that __tostring () is the method executed when the object is printed directly. we can use this method

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


6. _ sleep and _ wakeup
_ Used to serialize sleep
_ Called when wakeup is deserialized

During php Serialization, serialize () checks whether the class has _ sleep (). If yes, this function

The number will run before any serialization. This function must return a member attribute to be serialized and saved.

Array, and only serialize these member attributes returned by the function. this function has two functions: first, in order

Close any database connection that the object may have before the column is made. 2. specify the sequence in the object to be

If an attribute is large and does not need to be stored, you do not need to write it into _ sleep.

In the returned array, this attribute will not be serialized.

Conversely, after unserialize () creates an object from the byte stream, it immediately checks whether

The existence of The _ wakeup function. If yes, __wakeup is called immediately. Use the _ wakeup object

It re-creates any database connections that may be lost in serialization and processes other re-initialization tasks.

Class user
{
Public $ name;
Public $ id;

Function _ construct ()
{
$ This-> id = uniqid (); // assign a uniqid to a give user

Different IDs
}

Function _ sleep ()
{
Return (array ("name"); // do not serialize this-> id is not

Serial id
}

Function _ wakeup ()
{
$ This-> id = uniqid (); // give user a unique id
}
}

$ U = new user;
$ U-> name = "haha ";

$ S = serialize ($ u); // serialize it serializization note no string

The row id attribute is discarded.

$ U2 = unserialize ($ s); // The unserialize it deserialization id is

Assign a value again


// $ U and $ u2 have different ids $ u and $ u2 have different ids
Var_dump ($ u );
Var_dump ($ u2 );
?>

---------- Php debug ----------
Object (user) #1 (2 ){
["Name"] =>
String (4) "haha"
["Id"] =>
String (13) "47fa045529f69"
}
Object (user) #2 (2 ){
["Name"] =>
String (4) "haha"
["Id"] =>
String (13) "47fa04552a49a"
}


Explain 1. _ call ($ method, $ arg_array) when an undefined method is called, a special method _ call () is added to the object in php Tutorial 5. this method...

Related Article

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.