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:
<?PHPclasshandsonboy{Private $name= ' Chenqionghe '; Private $age= 18; Public function__call ($name,$arguments) { Switch(Count($arguments)) { Case2:Echo $arguments[0] *$arguments[1],Php_eol; Break; Case3:Echo Array_sum($arguments),Php_eol; Break; default:Echo' Wrong parameter ',Php_eol; Break; } }}$a=NewHandsonboy ();$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 classactiverecord{protected Static $table; protected $fieldvalue; Public $select; Static functionFindByID ($id) { $query= "SELECT * from".Static::$table. "WHERE id=$id"; returnSelf::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. The WHERE$field=$args[0] ' "; returnSelf::createdomain ($query); } Private Static functionCreateDomain ($query) { $class= Get_called_class ();//gets the class name of the static method call $domain=New $class(); $domain->fieldvalues =Array(); $domain->select =$query; foreach($class::$fields as $field=$type) { $domain->fieldvalues[$field] = ' todo:set from SQL result by '.$field; } return $domain; }}classCustomerextendsactiverecord{protected Static $table= ' Custdb '; protected Static $fields=Array( ' id ' = ' int ', ' email ' = ' int ', ' lastname ' = ' varchar ' );}classSalesextendsactiverecord{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);
Reference: https://jingyan.baidu.com/article/48a42057fd2890a92425043c.html
Ext.: HTTPS://WWW.CNBLOGS.COM/CHENQIONGHE/P/4735703.HTML?TVD
Reference: http://www.php.net/manual/zh/language.oop5.magic.php
__call and __callstatic Methods in PHP (not finished)