Defining a method's chained methods

0

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 this point I could chain the methods as follows

$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 "my dog", but I would like certain methods to be inaccessible, ie if I call the "cat" method I would like only the "mia" method to be accessible to chain.

  

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

    
asked by anonymous 04.07.2018 / 00:57

1 answer

4

It does not matter, because this concept is totally wrong. Cat and dog can not be inside Animal, they are animals, they are not part of an animal. And the concept is wrong, everything else will be wrong and trying to do thing to get it right is just creating a new error on top of the existing error.

Although I am critical not only of these abstract examples that teach nothing, or even cause evil, and that OO in business rules is usually a dubious practice, and yet PHP is not the most appropriate language to do this type of thing, much less the language that needs this kind of thing, I'd say the solution is to create an inheritance by taking animal as a basis abstract to not be instantiated, and then the derivatives of each animal have the method that makes sense. Although in this particular case inheritance will make very little sense because it even allows for reuse and polymorphism. It would still do to indicate subtype, but that is not strictly an inheritance.

It's not a matter of being more organized, it's a question of being right, something completely meaningless does not have to do this, and since this does not bring any advantage to make the hypothesis does not make sense.

If you still want to insist on this, what you can do is to have a field that stores the object that the animal is representing, in this case you are changing the inheritance by the composition, there it will have a Gato object or a Cachorro of the object Animal , and when calling the specific object you can only call the methods that are 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 a time, it may be up to an array . But it still seems crazy to me unless the class calls zoologico or something, which still does not seem very suitable.

    
04.07.2018 / 01:04