Use the __get method inside the controller

Asked

Viewed 41 times

0

In my entity I have the following magic method:

public function __get($key)
{
    return null;
}

On my controller I have the following code:

$clients = $this
    ->getDoctrine()
    ->getRepository("AppBundle:Clients")
    ->findAll()
;

//...

$naoExiste = $app->getNaoexiste(),

Despite the method __get exist within the entity, the following error occurs:

Attempted to call an Undefined method named "getNaoexiste" of class "Appbundle Entity Clients".

The method __get does not work in the entities?

1 answer

0

The method for calling methods that does not exist is not the __get, is the __call.

__get is to call an action when a property does not exist.

__call is to call an action when a method does not exist.

You should also take care when adding "magic features", because if Doctrine, internally check the method with method_exists, he will return false.

  • Thank you Wallace, I really changed the methods. Regarding the Doctrine, in what situation the verification of the method with method_exists occurs? I currently have no property with true/false values, but if there could be assigned the value false erroneously, that’s what you mean?

Browser other questions tagged

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