PHP accesses non-existent properties without reporting an error?

WBOY
Release: 2016-10-10 11:56:04
Original
1890 people have browsed it

Code:

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

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


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

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>
Copy after login
Copy after login

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.

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!