PHP magic function _ call () usage _ PHP Tutorial

Source: Internet
Author: User
PHP magic function _ call () usage. The so-called PHP magic function, simply put, has a specific name in PHP-it all starts with two underscores, in addition, the PHP interpreter will automatically search for the so-called PHP magic function when running at a certain time. Simply put, it has a specific name in PHP -- it starts with two underscores, in addition, the PHP interpreter will automatically find and run the methods at a certain time. The most common Magic function is of course the constructor method: __construct.

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:

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:

$ 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:

......
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:
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:
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];
}

// 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 ();......

...

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.