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();
    ?>

     

     

  • 相关阅读:
    curl命令详解
    Linux 下 set env export declare浅浅 set和shopt命令详解--(shell定制) (转载)
    ps aux指令詳解
    smb设置参考手册 --详细参数
    Ajax
    JSON浅谈
    Date类型
    笔记本电脑不能上网的问题
    editplus 初步设置
    w10 系统升级
  • 原文地址:https://www.cnblogs.com/Athrun/p/php__call.html
Copyright © 2011-2022 走看看