calling class method (with constructors) without object instantiation in php

前端 未结 4 1332
孤街浪徒
孤街浪徒 2020-12-16 04:15

Ive looked and tried but I can\'t find an answer.

In PHP, is it possible to call a class\' member function (when that class requires a constructor to receive parame

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 04:48

    Unfortunately PHP doesn't have support to do this, but you are a creative and look guy :D

    You can use an "factory", sample:

    __aaa = $aaa;
       }
    
       public static function factory($aaa)
       {
          return new Foo($aaa);
       }
    
       public function doX()
       {
          return $this->__aaa * 2;
       }
    }
    
    Foo::factory(10)->doX();   // outputs 20
    

提交回复
热议问题