Pergunta

Why is it that getting a property of any PHP primitive data type does not trigger an error or warning?

Tried this on php 5.3.28:

$num = 1;
$str = 'hello';
$arr = array(1,2,3);
$nada = null;

$num->key1;
$str->key2;
$arr->key3;
$nada->key4;

No errors or warnings were triggered.

Foi útil?

Solução

You don't have any error reporting enabled. When I run your code I see this:

PHP Notice:  Trying to get property of non-object in /home/5BSpSI/prog.php on line 8
PHP Notice:  Trying to get property of non-object in /home/5BSpSI/prog.php on line 9
PHP Notice:  Trying to get property of non-object in /home/5BSpSI/prog.php on line 10
PHP Notice:  Trying to get property of non-object in /home/5BSpSI/prog.php on line 11

Try running the code again, but add error_reporting(E_ALL); as the first line

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top