PHP constructors and static functions

后端 未结 4 1297
野性不改
野性不改 2021-02-12 20:06

I\'m a bit confused on how constructors work in PHP.

I have a class with a constructor which gets called when I instantiate a new object.

$foo = new Foo(         


        
4条回答
  •  走了就别回头了
    2021-02-12 20:37

    I define class properties as array in a static method and call them via the method. I'm not sure if it's the best solution or not but works great.

    Example:

        class Foo
        {
          private static construct_method()
          {
            return [
              'one' => 1,
              'two' => 2
            ];
          }
    
          public static any_method()
          {
            return self::construct_method()['one'] + self::construct_method()['two'];
          }
    
        }
    
        echo Foo::any_method(); // 3
    

提交回复
热议问题