Best practices for static constructors

后端 未结 8 2449
无人及你
无人及你 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

    The naming of any method should be with intention revealing names. I can't tell what 'Foo::factory' does. Try to build to a higher level language:

    User::with100StartingPoints();
    

    This would be the same as:

    $user = new User();
    $user->setPointsTo(100);
    

    You could also easily test whether User::with100StartingPoints() is equal to this.

提交回复
热议问题