Use of chained static functions

-1

I wanted to use static PHP functions as follows:

class A {
    static function a1() {
        echo "A1";
        return A;
    }
    static function a2() {
        echo "A2";
        return A;
    }
}

A::a1()::a2();

It works but shows this error:

  

A1

     

NOTICE Use of undefined constant A - assumed 'A' on line number 4

     

A2

     

NOTICE Use of undefined constant A - assumed 'A' on line number 8

What is assumed 'A' ?

Should I return the class in another way, or create a constant with the value of class A? In case, how?

Is it correct to say that these functions follow the Builder pattern even not being constructors?

    
asked by anonymous 07.08.2018 / 21:58

2 answers

-1

To use chained static functions, you must return __CLASS__ , for example:

class A {
    static function a1() {
        echo "A1";
        return __CLASS__;
    }
    static function a2() {
        echo "A2";
        return __CLASS__;
    }
}

A::a1()::a2();
    
22.09.2018 / 19:34
0

Pattern Builder has nothing to do with what you want. You want to make use of what is called fluent interface that is obtained through Setting method . Builder can be used together with Method Chaining to do whatever you want with objects.

If you want to use a builder something will be built. Your code is building nothing. And in fact what is static means that it makes little sense to build because there will be only one in the whole application, it is easier to do otherwise. This is usually done in creating objects.

As far as I know PHP does not even allow static method to have an object (which is not even the intent of this code) as its argument. This is called Uniform Function Call Syntax . Not having this mechanism is impossible to do what you want, could do:

A::a2(A::a1(objeto));

Note the use of an object that in theory these two methods will know what to do with it.

I did not even merit using this kind of mechanism in PHP, which makes very little sense. Including class use only with static methods.

And for me, this type of construct is language smell , or code smell when language has a better mechanism.

    
07.08.2018 / 22:53