__call and __callstatic Methods in PHP

Source: Internet
Author: User

How to prevent errors by calling methods that do not exist, using the __call magic reload method.

The __call method is prototyped as follows: Mixed __call (string $name, array $arguments) the __call () is called when an inaccessible method is called, such as undefined or invisible. where $ The name parameter is the names of the methods to invoke. The $arguments parameter is an array of arguments that the containing person will pass to the method, as follows:
<?phpclass handsonboy{    Private $name = ' Chenqionghe ';    Private $age =;    Public Function __call ($name, $arguments)    {        switch (count ($arguments))        {case            2:                echo $arguments [0] * $arguments [1],php_eol;                break;            Case 3:                echo array_sum ($arguments), Php_eol;                break;            Default:                echo ' parameter is wrong ', php_eol;                Break;}}}    $a = new Handsonboy (), $a->make (5), $a->make (5,6);
The above code simulates overloading in similar other languages based on the parameter type. The Rubik's Cube method with __call is __callstatic. Of course, using the Magic method "to prevent calls to the non-existent method surface error" is not the essence of the Magic method. In fact, The Magic method uses the dynamic creation of methods to become possible. This is a useful syntax in framework design such as MVC. If a controller calls a method that does not exist, it can be handled very nicely by defining the __call magic method. The following code uses _ Callstatic This magic method for dynamic creation and delay binding of methods to implement a simple ORM model
<?phpabstract class activerecord{protected static $table;    protected $fieldvalue;    Public $select; static function FindByID ($id) {$query = "select * from". Static:: $table.        "WHERE id= $id";    Return Self::createdomain ($query);    } function __get ($fieldname) {return $this->fieldvalues[$fieldname];        } static function __callstatic ($method, $args) {$field = Preg_replace ('/^findby (\w*) $/', ' $ ', $method); $query = "SELECT * from". Static:: $table.        "WHERE $field = ' $args [0] '";    Return Self::createdomain ($query); The private static function CreateDomain ($query) {$class = Get_called_class ();//Gets the class name of the static method call $domain = N        EW $class ();        $domain->fieldvalues = Array ();        $domain->select = $query; foreach ($class:: $fields as $field = + $type) {$domain->fieldvalues[$field] = ' todo:set from SQL R Esult by '.        $field;    } return $domain; }}class Customer extends activerecord{protected static $table = ' Custdb ';    protected static $fields = array (' id ' = ' = ' int ', ' email ' = ' int ', ' lastname ' = ' varchar ') );}    Class Sales extends activerecord{protected static $table = ' SalesDB '; protected static $fields = array (' id ' = ' = ' int ', ' item ' = ' varchar ', ' qty ' = ' = ' int ');} Var_dump (Customer::findbyid (123)->select) Var_dump (Customer::findbyid (123)->email); Var_dump (Sales:: Findbylastname (' Denoncourt ')->select);

__call and __callstatic Methods in PHP

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.