php类的成员变量没有声明解决思路

WBOY
Release: 2016-06-13 11:40:00
Original
880 people have browsed it

php类的成员变量没有声明

<br />class test{<br />    private $name;<br />    private $sex;<br />    function __construct(){<br />        $this->aaa='aaa';<br />    }<br />}<br /><br />$test=new test();<br /><br />$reflect=new ReflectionClass($test);<br />$pro=$reflect->getDefaultProperties();<br />print_r($pro);//打印结果:Array ( [name] => [sex] => )<br /><br />echo $test->aaa;//打印结果:aaa<br />
Copy after login


在这个test类中,声明了两个成员变量$name和$sex,但是在构造函数中,又声明了一个变量$aaa,初始化类,使用反射类打印默认成员属性只有声明的两个成员变量属性,但是打印类的$aaa变量发现还是可以输出结果。

请问类的成员变量不用声明,在函数中声明也是可以的吗,有什么区别?

php 面向对象
Related labels:
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!