• PHP重载


    参考:http://php.net/manual/zh/language.oop5.overloading.php#object.call

    1.定义

    PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法。

    2.实现

    通过魔术方法(magic methods)来实现的。

    3.触发条件

    调用当前环境下未定义或不可见的类属性或方法时,重载方法会被调用。

    4.属性重载

    注:属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 声明为 static。

    public void __set ( string $name , mixed $value )
    
    在给不可访问属性赋值时,__set() 会被调用。
    public mixed __get ( string $name )
    
    读取不可访问属性的值时,__get() 会被调用。
    public bool __isset ( string $name )
    
    当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用。
    public void __unset ( string $name )
    
    当对不可访问属性调用 unset() 时,__unset() 会被调用。
    <?php
    class PropertyTest {
         /**  被重载的数据保存在此  */
        private $data = array();
    
     
         /**  重载不能被用在已经定义的属性  */
        public $declared = 1;
    
         /**  只有从类外部访问这个属性时,重载才会发生 */
        private $hidden = 2;
    
        public function __set($name, $value) 
        {
            echo "Setting '$name' to '$value'
    ";
            $this->data[$name] = $value;
        }
    
        public function __get($name) 
        {
            echo "Getting '$name'
    ";
            if (array_key_exists($name, $this->data)) {
                return $this->data[$name];
            }
    
            $trace = debug_backtrace();
            trigger_error(
                'Undefined property via __get(): ' . $name .
                ' in ' . $trace[0]['file'] .
                ' on line ' . $trace[0]['line'],
                E_USER_NOTICE);
            return null;
        }
    
        /**  PHP 5.1.0之后版本 */
        public function __isset($name) 
        {
            echo "Is '$name' set?
    ";
            return isset($this->data[$name]);
        }
    
        /**  PHP 5.1.0之后版本 */
        public function __unset($name) 
        {
            echo "Unsetting '$name'
    ";
            unset($this->data[$name]);
        }
    
        /**  非魔术方法  */
        public function getHidden() 
        {
            return $this->hidden;
        }
    }
    
    
    echo "<pre>
    ";
    
    $obj = new PropertyTest;
    
    $obj->a = 1;
    echo $obj->a . "
    
    ";
    
    var_dump(isset($obj->a));
    unset($obj->a);
    var_dump(isset($obj->a));
    echo "
    ";
    
    echo $obj->declared . "
    
    ";
    
    echo "Let's experiment with the private property named 'hidden':
    ";
    echo "Privates are visible inside the class, so __get() not used...
    ";
    echo $obj->getHidden() . "
    ";
    echo "Privates not visible outside of class, so __get() is used...
    ";
    echo $obj->hidden . "
    ";
    ?>
    
    //输出结果
    Setting 'a' to '1'
    Getting 'a'
    1
    
    Is 'a' set?
    bool(true)
    Unsetting 'a'
    Is 'a' set?
    bool(false)
    
    1
    
    Let's experiment with the private property named 'hidden':
    Privates are visible inside the class, so __get() not used...
    2
    Privates not visible outside of class, so __get() is used...
    Getting 'hidden'
    
    Notice: Undefined property via __get(): hidden in /usercode/file.php on line 75 in /usercode/file.php on line 31
    属性重载

    5.方法重载(所有的重载方法都必须被声明为 public

    public mixed __call ( string $name , array $arguments )
    
    在对象中调用一个不可访问方法时,__call() 会被调用。
    public static mixed __callStatic ( string $name , array $arguments )
    
    在静态上下文中调用一个不可访问方法时,__callStatic() 会被调用。

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

    <?php
    class MethodTest 
    {
        public function __call($name, $arguments) 
        {
            // 注意: $name 的值区分大小写
            echo "Calling object method '$name' "
                 . implode(', ', $arguments). "
    ";
        }
    
        /**  PHP 5.3.0之后版本  */
        public static function __callStatic($name, $arguments) 
        {
            // 注意: $name 的值区分大小写
            echo "Calling static method '$name' "
                 . implode(', ', $arguments). "
    ";
        }
    }
    
    $obj = new MethodTest;
    $obj->runTest('in object context');
    
    MethodTest::runTest('in static context');  // PHP 5.3.0之后版本
    ?>
    
    //输出结果
    Calling object method 'runTest' in object context
    Calling static method 'runTest' in static context
    方法重载

     6.应用(以转账功能为例,参考:https://www.jianshu.com/p/25a78620fa5c

    需求:
    1.系统与用户间的转账:需传入接收方和金额
    2.用户与用户间的转账:需传入接收方、发送方、金额和留言
    3.其他
    
    解决思路:
    1.为了应对需求的变化,剥离与具体实现无关的业务模型到Transfer类,将具体的实现放入Builder类。
    2.针对每个原子功能的方法在Builder类实现。
    3.由Transfer去调用Builder构造需要的转账类型,完成相关操作。
    4.当需求变化时(要增加新的功能时),只要在Builder里添加相应的方法即可,而不用改动现有代码。

    具体实现代码:

    class Transfer
    {
        public function __call($method, $parameters)
        {
            $builder = new Builder();
            return call_user_func_array([$builder, $method], $parameters);
        }
    
        public static function __callStatic($method, $parameters)
        {
            $instance = new static;
            return call_user_func_array([$instance, $method], $parameters);
        }
    }
    Transfer
    class Builder
    {
        protected $from = 0; // 0 represents system
        protected $to = 0;
        protected $amount = 0;
        protected $comments = '';
        protected $related = [];
    
        public function from($user)
        {
            if ($user instanceof User) {
                $this->from = $user->getAuthIdentifier();
            } elseif (is_int($user)) {
                $this->from = $user;
            } else {
                throw new InvalidArgumentException(sprintf('%s excepts $user parameter to be AppUser or integer, %s given.', __METHOD__, gettype($user)));
            }
            return $this;
        }
        public function to($user){...}
        public function amount(int $amount){...}
        public function comments($comments){...}
        public function related(int $type, int $id, $extra = null){...}
        public function transfer(){...}
    }
    Builder
    //组合转账功能
    
    Transfer::from($sender)->to($receiver)->amount($amount)->comments($comments)->related($related_type, $related_id, $related_extra)->transfer();
  • 相关阅读:
    iOS 阶段学习第23天笔记(XML数据格式介绍)
    iOS 阶段学习第22天笔记(JSON数据格式介绍)
    iOS阶段学习第21天笔记(ARC内存管理-Copy-代理)
    iOS阶段学习第20天笔记(MRC内存管理)
    iOS阶段学习第19天笔记(协议-Protocol)
    iOS阶段学习第18天笔记(Plist-Archiver-归档与解归档操作)
    iOS阶段学习第17天笔记(NSFileManager-NSFileHandle-文件操作)
    Mac系统与Xcode的一些常用操作介绍
    iOS阶段学习第16天笔记(Category-NSSet-SEL-NSIndexSet 操作)
    iOS UIButton添加圆角,添加边框
  • 原文地址:https://www.cnblogs.com/yueyun00/p/10044363.html
Copyright © 2020-2023  润新知