Home  >  Article  >  Backend Development  >  PHP accesses non-existent properties without reporting an error?

PHP accesses non-existent properties without reporting an error?

WBOY
WBOYOriginal
2016-10-10 11:56:041886browse

Code:

<code><?php
error_reporting(E_ALL | E_STRICT);

class Father {
    private $name = 'meng';
}


$father = new Father;
$father->sex = 'male';
</code>

Then execute:

PHP accesses non-existent properties without reporting an error?
No questions asked. . .

PHP accesses non-existent properties without reporting an error?

Reply content:

Code:

<code><?php
error_reporting(E_ALL | E_STRICT);

class Father {
    private $name = 'meng';
}


$father = new Father;
$father->sex = 'male';
</code>

Then execute:

PHP accesses non-existent properties without reporting an error?
No problem. . .

PHP accesses non-existent properties without reporting an error?

PHP does not force attributes to be declared in the class. Writing it like this is actually equivalent to dynamically adding attributes to the object, but it is best not to write it like this, because maybe one object will be missed.

This is to dynamically add attributes to an object (instance of a class), and no error will be reported. However, if you access the attributes instead of adding attributes, an error will be reported. For example, if you are echo $father->sex and It's not that $father->sex = 'male' will report an error.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn