Use of chained static functions

Asked

Viewed 217 times

0

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();

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 this case, how?

It is correct to say that these functions follow the same Builder standard nay being builders?

  • 1

    It’s kind of weird, what the need to be static?

  • The methods would be to change the response status and to return an object or array that will be converted to json, I first did together, but would like to separate into two parts

  • 1

    Related: What is Chaining of Methods?, Defining the chained methods of a method and What is Fluent Interface?. Maybe the first link answers your question.

  • @rray thanks for the links, the first only lacked show with static functions, I will read better later

  • return A would return a predefined constant - so the error.

  • @Guilhermecostamilam Has the answer solved what was in doubt? Do you need something better? Do you think it is possible to accept it now?

  • @Maniero solved the problem by returning __CLASS__ in functions, your answer answers the second question but not the first, if you want to edit it with this solution I accept it

Show 2 more comments

2 answers

1

Pattern Builder has nothing to do with what you want. You want to make use of what is called fluent interface which is obtained through method chaining. The Builder can be used together with the Method Chaining to do what 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 something static makes very 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 the creation of objects.

As far as I know PHP even allows static method to have an object (which is not even the intention 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));

I put in the Github for future reference.

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

I didn’t even get into the merit of using this kind of mechanism in PHP, which makes very little sense. Including the use of class only with static methods.

And to me that kind of construction is Smell language, or code Smell when the language has better mechanism.

  • The use of class makes sense, besides these have other methods and private variables. I did not understand why to pass the function result as another parameter, could explain more?

  • I don’t know, your code makes less sense yet, I just tried to make a little bit of sense to it, still it’s bad, as I said in the answer. Anyone who says something makes sense has to explain what the sense is, has neither in the question nor in the comments.

  • The code is part of a routing library that I am creating, so I want to leave all the functions inside the class, which has private variables with the defined routes, settings, etc. These methods would serve to give an answer (one changes the status and the other two the body). Could just do Router::status(404); Router::json(array("error" => "Página não encontrada")); but I wanted it to be more direct and separate into two different methods, which may or may not be combined. As I am basing myself on the Express of Node I seek to do something as close as possible

0


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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.