Help guys to understand when the following error occurs.

$culture = $this->context->getUser()->getCulture(); //Выдает ошибку Call to a member function getCulture() on null //Пытаюсь решить следующим образом $user = $this->context->getUser(); if($user) $culture = $user->getCulture(); else $culture = 'ru'; //бесполезно :( $this->inst_details['name'] = $this->instrument->getName(); //Выдает ошибку Call to a member function getName() on boolean in ... 

It is interesting that this is a mistake I brought out of the logs. So I did not meet her. It seems to work. But there is some situation when these errors occur.

1) Call to a member function foo () on null | on boolean in which cases PHP gives such errors

2) in what cases in symfony 1.4 $ this-> context-> getUser () -> getCulture (); may produce this error.

Help please understand and solve this problem.

    1 answer 1

    1) Call to a member function foo () on null | on boolean in which cases PHP gives such errors

    When you try to call a method of an object, but instead of the object itself, you have null or false. This happens when you do not handle errors and do not check what is the result of the function that creates your object.

    For example, you try to read the first line from a file, but do not check before it that the file exists - the system will generate an error that you are trying to perform an invalid operation. (This is an abstract example to illustrate.)

    _

    2) in what cases in symfony 1.4 $ this-> context-> getUser () -> getCulture (); may produce this error.

    When the getUser () method returns null, instead of a user object. When to use unauthorized, for example:

    It's important to check if the user is authenticated first. If you aren’t the string anon .. Wait, what? Yes, this is a quirk. If you’re not logged in, you can use the controller shortcut converts.

    If you’re a user, you’ll be able to use this method.

    http://symfony.com/doc/2.8/security.html#always-check-if-the-user-is-logged-in

    In Symfony 1.4 getUser () method can just return null, in your case, this is what happens: https://github.com/symfony/symfony1/blob/75d9b19c845d29f6d1809d5bfc9b31d27cc489b6/lib/util/sfContext.class.php#L431

    • your link for symfony3.1. when instead of the object itself null or false gives an error on a none-object . In symfony1.4 $this->context->getUser()->getCulture(); works fine if the user is not authorized by symfony.com/legacy/doc/gentle-introduction/1_4/en/… - user216109
    • Perhaps, but this does not change the meaning - the error is caused by the fact that the $this->context->getUser() method returned not the user object, but null - Alexey Ukolov
    • And it seems to me that NULL is transmitted to him And an error about it - user216109
    • It seems to you wrong. To "him" null is passed? - Alexey Ukolov
    • The error message says “Attempt to call getCulture method to null”. I added a link to the source code for symfony 1.4 in the answer so you can be sure. - Alexey Ukolov