PHP magic function _ call () usage

Source: Internet
Author: User
Tags constructor

Method calls in PHP work like this. First, the PHP interpreter looks for methods on the class. If a method exists, PHP calls it. If not, call the magic function _ call in the class (if this method exists ). If _ call fails, the parent class method is called, and so on.

In this case, the white teeth seem to be a little abnormal. Let's take an example and look at the following code:

The code is as follows: Copy code

Class test {
Public function _ construct (){
Echo "this is construct! N ";
}
}
......

In this test class, only one constructor outputs some dispensable spam characters, nothing else;

At this time, let's instantiate it and call an atomic bomb method. What do you think of him? We immediately did this, and looked:

The code is as follows: Copy code

$ Send = new test ();
$ Send-> atomBomb ();

The result is conceivable. He will certainly tell you that there is no such method-we do not have this atomic bomb method! The error message is as follows:
Debug Error: test. php line 9-Call to undefined method test: atomBomb ()

Let's modify this class and add a _ call method. How can this problem be solved:

The code is as follows: Copy code

......
Class test {
Public function _ construct (){
Echo "this is construct! N ";
}

Public function _ call ($ name, $ arg ){
Echo "function name:", $ name, "n arg:". $ arg;
}
}
......

Repeat the preceding call method:
$ Send = new test ();
$ Send-> atomBomb ('AB', 9 );

The result is definitely different from the previous one. The result is as follows:

This is construct! // This is output by the constructor.

// These are output by the _ call function below

The code is as follows: Copy code
Function name: atomBomb
Arg: Array

In addition, we can easily see that __call has two parameters: the first parameter: the name of the called method, and the second parameter: the parameter entered when the method is called (this is an array ).

If you say so much, you don't know. If you do, you will certainly ask what is the use of this thing? What can we do?

Let me give you a idea to use it. What can I do! Imagine how many classes do you need to write if you treat all tables in a database as objects and perform CURD operations on them? Of course, if your array library has only two tables, you can tell me that there are only two classes! But what if there are 108 tables (for example, dede is 108 tables), and manually enter 108 classes? Obviously not scientific. What is the most expensive in the 21st century? -- Time!

We can write a class, but the rest of them are automatically created. I found some code in IBM and simplified it. You can see that this is something written by senior engineers.

 

The code is as follows: Copy code
Class DBObject {
Private $ id = 0;
Private $ table;
Private $ fields = array ();
Function _ construct ($ table, $ fields ){
$ This-> table = $ table;
Foreach ($ fields as $ key)
$ This-> fields [$ key] = null;
}
Function _ call ($ method, $ args ){
If (preg_match ("/set _ (. *)/", $ method, $ found )){
If (array_key_exists ($ found [1], $ this-> fields )){
$ This-> fields [$ found [1] = $ args [0];
Return true;
}
}
Else if (preg_match ("/get _ (. *)/", $ method, $ found )){
If (array_key_exists ($ found [1], $ this-> fields )){
Return $ this-> fields [$ found [1];
}
}
Return false;
}
Function insert (){
Global $ db;
$ Fields = $ this-> table. "_ id ,";
$ Fields. = join (",", array_keys ($ this-> fields ));
$ Inspoints = array ("0 ");
Foreach (array_keys ($ this-> fields) as $ field)
$ Inspoints [] = "? ";
$ Inspt = join (",", $ inspoints );
$ SQL = "INSERT INTO". $ this-> table. "($ fields) VALUES ($ inspt )";
$ Values = array ();
Foreach (array_keys ($ this-> fields) as $ field)
$ Values [] = $ this-> fields [$ field];
$ Something = $ db-> prepare ($ SQL );
$ Db-> execute ($ TH, $ values );
$ Res = $ db-> query ("SELECT last_insert_id ()");
$ Res-> fetchInto ($ row );
$ This-> id = $ row [0];
Return $ row [0];
} </Code>
 
// The following three methods are deleted: update, delete, and delete all (Battlefield note)
}
$ Book = new DBObject ('book', array ('author ',
'Title', 'Her her '));
$ Book-> delete_all ();
$ Book-> set_title ("PHP Hacks ");
$ Book-> set_author ("Jack Herrington ");
$ Book-> set_publisher ("O 'Reilly ");
$ Id = $ book-> insert ();
Echo ("New book id = $ idn ");
$ Book-> set_title ("Podcasting Hacks ");
$ Book-> update ();
$ Book2 = new DBObject ('book', array ('author ',
'Title', 'Her her '));
$ Book2-> load ($ id );
Echo ("Title =". $ book2-> get_title (). "n ");
$ Book2-> delete ();......
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.