zoukankan      html  css  js  c++  java
  • [资料]PHP中的__call使用

    PHP中的__call使用

    官方文档:http://cn2.php.net/__call

    public mixed __call ( string $name , array $arguments )
    public static mixed __callStatic ( string $name , array $arguments )
    当调用一个不可访问方法(如未定义,或者不可见)时,__call() 会被调用。

    当在静态方法中调用一个不可访问方法(如未定义,或者不可见)时,__callStatic() 会被调用。

    $name 参数是要调用的方法名称。$arguments 参数是一个数组,包含着要传递给方法$name 的参数。

    Demo 1

    <?php
    class PersonWriter {
    
        function writeName( Person $p ) {
            print $p->getName()."\n";
        }
    
        function writeAge( Person $p ) {
            print $p->getAge()."\n";
        }
    }
    
    class Person {
        private $writer;
    
        function __construct( PersonWriter $writer ) {
            $this->writer = $writer;
        }
    
        function __call( $method, $args ) {
            if ( method_exists( $this->writer, $method ) ) {
                return $this->writer->$method( $this );
            }
        }
    
        function getName()  { return "Bob"; }
        function getAge() { return 44; }
    }
    
    $person= new Person( new PersonWriter() );
    $person->writeName();
    $person->writeAge();
    ?>

     

     

  • 相关阅读:
    13-Smell味道-调味料
    12-Juice饮料
    11-Snack小吃
    10-Meat肉类
    09-Fruit水果
    08-Vegetables蔬菜
    [svc]共享内存
    [sql]sql的select字符串切割
    [js] 渲染树构建、布局及绘制
    [js]变量提升-关于条件
  • 原文地址:https://www.cnblogs.com/Athrun/p/php__call.html
Copyright © 2011-2022 走看看