User identity and undefined properties

Notice: This thread is very old.
MartyIX
Member | 217
+
0
-

Hi,

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:

https://api.nette.org/…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
$myModel->computeWage($this->getUser()->getIdentity()->nonExistingProperty);
// $identity->nonExistingProperty is now defined with NULL value!

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

Thanks

EDIT: Nette 2.0.12
EDIT2:

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";
var_dump(getVal());
echo "second example\n";
var_dump("second_example", getVal2());

Outputs:

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

The ampersand here https://api.nette.org/…ity.php.html#132 prevents PHP from generating of E_NOTICE errors.

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