Home  >  Article  >  Backend Development  >  Reference for learning about PHP's __call

Reference for learning about PHP's __call

WBOY
WBOYOriginal
2016-07-25 08:58:41924browse
  1. class Caller

  2. {
  3. private $x = array(1, 2, 3);

  4. public function __call($m, $a)

  5. {
  6. print "Method $m called:n";
  7. var_dump($a);
  8. return $this->x;
  9. }
  10. }

  11. $foo = new Caller();

  12. $a = $foo->test(1, "2", 3.4, true);
  13. var_dump($a);

Copy code

Code description: The first parameter $m of __call above is the method name test you want to call. The second parameter is the parameter you passed when calling the method and is passed in as data.

Output result:

————- php ——- Method test called: array(4) { [0]=> int(1) [1]=> string(1) “2″ [2]=> float(3.4) [3]=> bool(true) } array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

Output completed (took 0 seconds) – normal termination

What is the purpose of this function? Can it automatically obtain parameters? Automatically load n multiple tables of the database? Let’s slowly ponder and experience it while using it.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn