Defining the chained methods of a method

Asked

Viewed 56 times

3

Let’s say I have the following class:

class animal{

    private $animal;
    private $som;

    function gato(){
        $this->animal = 'gato';
        return $this;
    }

    function cachorro(){
        $this->animal = 'cachorro';
        return $this;
    }

    function mia()
    {
        $this->som = 'miau';
        return $this;
    }

    function late()
    {
        $this->som = 'au au';
        return $this;
    }
}

From that point I could chain the methods in the following way

$animal = new animal();

$animal->gato()->mia();
$animal->cachorro()->late();

Or else:

$animal = new animal();

$animal->gato()->late();
$animal->cachorro()->mia();

As you can see above, by the code I said the "mia dog", but I would like to make certain methods inaccessible, that is, if I call the method "cat" I would like only the method "mia" to be accessible to chain.

I know that if I divide the methods into different classes it is easier to accomplish this task, even more organized, but I want to know if it is possible to perform such a task within the same class.

1 answer

6


I cannot, because this concept is totally wrong. Cat and dog can’t be inside Animal, they’re animals, they’re not part of an animal. And the concept is wrong, everything else will be wrong and try to do thing to get it right just create a new mistake on top of the existing error.

Although I am critical not only of these abstract examples that teach nothing, or even cause harm, and that oo in business rules is often a dubious practice, and although PHP is not the most appropriate language to do this kind of thing, much less the language that needs this kind of thing, I would say that the solution is to create an inheritance taking animal as a basis abstract not to let it be instantiated, and then the derivatives of each animal have the method that makes sense. Although in this specific case the inheritance will make little sense because it even allows reuse and polymorphism. Still would to indicate subtype, but that is not strictly an inheritance.

It’s not a question of being more organized, it’s a question of being right, with something completely meaningless you don’t have to do this, and since it doesn’t bring any advantage until you make the hypothesis doesn’t make sense.

If you still want to insist on this, what you can do is have a field that holds the object that this animal is representing, in this case it is exchanging the inheritance for the composition, there will be an object Gato or an object Cachorro inside the object Animal, and when calling the specific object you can only call the methods inside it.

This eliminates inheritance, but not the need to have objects created separately.

You may even have more than one field to contain more than one object type at the same time, it may even be a array. But it still seems crazy to me unless the class calls Zoologico or something like that, which still doesn’t seem very appropriate.

  • Thank you, and in fact I have no need to do this, but I was reading about chained methods and I was left with doubt whether this would be possible

  • 1

    The chained method itself is no problem, the problem is selecting what you can or can’t access, even more in PHP.

Browser other questions tagged

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