PHP property as object

前端 未结 4 1899
感情败类
感情败类 2021-01-07 01:44

Is it possible to set a property of a class as a object?

Like:

class User {

    public $x = \"\";
    public $y = new ErrorVO();
    public $w = new         


        
相关标签:
4条回答
  • 2021-01-07 02:39

    Just my preferred solution, even if the others already explain everything: Injection

    class A {
      public $a;
      public function __construct (ErrorVO $a) {
        $this->a = $a;
      }
    }
    

    This keeps the class testable and allows to replace the wanted ErrorVO-implementation very easy. Of course you can combine both solutions into one

    class A {
      public $a;
      public function __construct (ErrorVO $a = null) {
        $this->a = is_null($a) ? new ErrorVO : $a;
      }
    }
    

    Minor Update: In the meantime you can write the second example like this

    class A {
      public $a;
      public function __construct (ErrorVO $a = null) {
        $this->a = $a ?: new ErrorVO;
      }
    }
    

    It's slightly more compact and imo it makes the intention more clear. Compare the ?:-operator with MySQLs COALESCE

    0 讨论(0)
  • 2021-01-07 02:41

    In the constructor, yes.

    class User
    {
        public $x = "";
        public $y = null;
        public $w = array();
    
        public function __construct()
        {
            $this->y = new ErrorVO();
        }
    }
    

    Edit

    KingCrunch made a good point: You should not hard-code your dependencies. You should inject them to your objects (Inversion of Control (IoC)).

    class User
    {
        public $x = "";
        public $y = null;
        public $w = array();
    
        public function __construct(ErrorVO $y)
        {
            $this->y = $y;
        }
    }
    
    new User(new ErrorVD());
    
    0 讨论(0)
  • 2021-01-07 02:42

    Yes, it is. But this can be done during the class instantiation (in the constructor) or later, for example:

    • during instantiation:

      class A {
          public $a;
          public function __construct() {
              $this->$a = (object)array(
                  'property' => 'test',
              );
          }
      }
      
      $my_object = new A();
      echo $my_object->a->property; // should show 'test'
      
    • after instantiation:

      class B {
          public $b;
      }
      
      $my_object = new B();
      $my_object->b = (object)array(
          'property' => 'test',
      );
      echo $my_object->b->property; // should also show 'test'
      

    You can not assign object directly in class definition outside constructor or other methods, because you can do it only with scalars (eg. integers, strings etc.).

    0 讨论(0)
  • 2021-01-07 02:46

    You cannot declare them in the property declarations, but you can instantiate them in the constructor __construct() The array() can be instantiated in the public $w without the new keyword: public $w = array();

    public function __construct()
    {
      $this->y = new ErrorVO();
    }
    
    0 讨论(0)
提交回复
热议问题