Best practices for static constructors

后端 未结 8 2461
无人及你
无人及你 2020-12-13 14:41

I want to create an instance of a class and call a method on that instance, in a single line of code.

PHP won\'t allow calling a method on a regular constructor:

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 15:05

    Propel uses a static method "create". I'd go with that. This method makes the code easier to test rather than just using static methods to perform business logic.

    Besides, you can also pass parameters to the constructor. For instance:

    In either case, you'd be able to invoke myMethod right after the create method

    myMethod();
    // or
    MyClass::create($param1, $param2)->myMethod();
    

提交回复
热议问题