代码之家  ›  专栏  ›  技术社区  ›  Kiyarash

链方法的能力,无需强制新的关键字使用php

  •  10
  • Kiyarash  · 技术社区  · 7 年前

    我在写一个剧本 必须

    $father = Father::firstName('Esaaro')->lastName('Ozaaraa')->age(42);
    
    Person::firstName("Soobaasaa")->lastName( "Ozaaraa")->age(17)
      ->setFather( $father )-> toArray();
    

    Person Father .

    firstName static public

    这是我的文件结构

    <?php
    
    class Person
    {
        protected static $name;
        protected $lastName, $age, $father, $result;
    
        public static function firstName($name = null)
        {
            self::$name = $name;
        }
    
        public function lastName($lastName = null)
        {
            $this->lastName = $lastName;
        }
    
        public function age($age = null)
        {
            $this->age = $age;
        }
    
        public function toArray()
        {
    
        }
    
        public function setFather(Father $father)
        {
    
        }
    }
    
    
        /*
         * Father Class
         */
    
    class Father
    {
        protected static $name;
        protected $family, $age, $result;
    
        public static function firstName($name = null)
        {
            self::$name = $name;
        }
    
        public function lastName($lastName = null)
        {
            $this->family = $lastName;
        }
    
        public function age($age = null)
        {
            $this->age = $age;
        }
    
        public function toArray()
        {
            ( (isset(static::$name) && static::$name !== null) ? $this->result['firsName'] = self::$name : '' );
            ( (isset($this->family) && $this->family !== null) ? $this->result['lastName'] = $this->family : '' );
    
            return $this->result;
        }
    }
    

    上面的代码只是一个结构,我刚刚开始编写脚本。文件结构无法更改,因为这是一个挑战。

    提前谢谢

    2 回复  |  直到 7 年前
        1
  •  8
  •   Phil    7 年前

    实际上你所需要的只是静电 firstName

    设置器 $this 提供所谓的 fluent interface .

    如果创建实例的唯一方法是通过静态 名字 方法,您还需要添加一个私有/受保护的构造函数。

    例如

    class Person
    {
        private $firstName;
        private $lastName;
        private $age;
        private $father;
    
        private function __construct(string $firstName) {
            $this->firstName = $firstName;
        }
    
        public static function firstName(string $name) {
            return new Person($name);
        }
    
        public function lastName(string $lastName) {
            $this->lastName = $lastName;
            return $this;
        }
    
        public function age(int $age) {
            $this->age = $age;
            return $this;
        }
    
        public function setFather(Father $father) {
            $this->father = $father;
            return $this;
        }
    
        public function toArray() {
            // just an example
            return [
                'firstName' => $this->firstName,
                'lastName'  => $this->lastName,
                'age'       => $this->age,
                'father'    => $this->father->toArray(),
            ];
        }
    }
    

    $name 属性为静态。您不想更改一个实例的 $名称 private $firstName

        2
  •  6
  •   Aaron Saray    7 年前

    class Father
    {
        protected static $instance;
    
        protected $firstName = '';
        protected $lastName = '';
    
        public function __call($name, $arguments)
        {
            if (property_exists($this, $name)) {
                $this->$name = array_shift($arguments);
            }
    
            return $this;
        }
    
        public static function __callStatic($name, $arguments)
        {
            if (is_null(static::$instance)) {
                static::$instance = new static;
            }
    
            return static::$instance->$name(...$arguments);
        }
    
        public function toArray()
        {
            return [
                'firstName' => $this->firstName,
                'lastName' => $this->lastName
            ];
        }
    }
    

    在这个版本中,您可以以静态或非静态的方式调用以受保护变量命名的函数。所以,你可以 Father::firstName('dad')->lastName('saray') 反之亦然 Father::lastName('saray')->firstName('dad')

    根据评论的要求,发生了以下情况:

    firstName() lastName() 作为静态的,魔术的方法 __callStatic() 正在运行。如果它检测到没有实例,它将创建一个实例,调用该方法并返回该实例。以后的所有调用都将由magic方法处理 __call() 名字() 姓氏() 首先是静态的,然后所有后续调用都将是非静态的。

    推荐文章