User identity and undefined properties

Notice: This thread is very old.

6 years ago

Member | 219


I've found out that $this->getUser()->getIdentity()->nonExistingProperty returns NULL value. I would be much happier if it was throwing an E_NOTICE exception because it would prevent bugs introduced by typos. I checked source code and the culprit is the line:…ity.php.html#138

You can say that returning NULL if a property does not exist is a correct behaviour (SessionSection has the setting warnOnUndefined after all). Yes, I agree even though I don't really like it. However, it is interesting that a non-existing property (e.g. nonExistingProperty) is also added to the identity with NULL value as a consequence of using it. For example:

$identity = $this->getUser()->getIdentity();
// $identity->nonExistingProperty is not defined
// $identity->nonExistingProperty is now defined with NULL value!

Is it correct behaviour? This can lead to very-hard-to-spot bugs in my opinion.


EDIT: Nette 2.0.12

A test script to see what I mean:

error_reporting(E_ALL | E_STRICT);

function &getVal() {
   $data = [];

   return $data['hey'];

function getVal2() {
   $data = [];

   return $data['hey'];

echo "first example\n";
echo "second example\n";
var_dump("second_example", getVal2());


first example
second example
<br />
<b>Notice</b>:  Undefined index: hey in <b>[...][...]</b> on line <b>12</b><br />
string(14) "second_example"

The ampersand here…ity.php.html#132 prevents PHP from generating of E_NOTICE errors.

Last edited by MartyIX (2013-12-18 08:22)