get_class_vars() 未显示变量,但在同一类上运行的 property_exists() 返回 true
我正在学习 PHP,并且我已经开始使用类——下面可能是最基本的对象,哈哈。
<?php
class Person {
var $first_name;
var $last_name;
var $arm_count = 2;
var $leg_count = 2;
function say_hello() {
echo "Hello from inside the class " . get_class($this) .".<br />";
}
function full_name() {
return $this->first_name . " " . $this->last_name;
}
}
$person = new Person();
echo $person->arm_count . "<br />";
$person->first_name = 'Lucy';
$person->last_name = 'Ricardo';
echo $person->full_name() . "<br />";
$vars = get_class_vars('Person');
foreach($vars as $var => $value) {
echo "{$var}: {$value}<br />";
}
echo property_exists("person","first_name") ? 'true' : 'false';
?>
然后运行上面的代码,应该会输出一些数据。在课程中(Kevin Skoglund 的视频培训系列,“PHP:超越基础知识",) Kevin 的屏幕看起来正确(他使用的是 5.2.6。)
我在5.3 在我的 WAMP 安装中,Person 类的“first_name”属性没有被循环吐出...但是 echo property_exists("person","first_name") ? 'true' : 'false';
返回 true。
谁能帮助我理解出了什么问题?
I'm studying PHP, and I've started playing with classes--below is possibly the most basic object ever, lol.
<?php
class Person {
var $first_name;
var $last_name;
var $arm_count = 2;
var $leg_count = 2;
function say_hello() {
echo "Hello from inside the class " . get_class($this) .".<br />";
}
function full_name() {
return $this->first_name . " " . $this->last_name;
}
}
$person = new Person();
echo $person->arm_count . "<br />";
$person->first_name = 'Lucy';
$person->last_name = 'Ricardo';
echo $person->full_name() . "<br />";
$vars = get_class_vars('Person');
foreach($vars as $var => $value) {
echo "{$var}: {$value}<br />";
}
echo property_exists("person","first_name") ? 'true' : 'false';
?>
Then the above runs, it is supposed to output a bit of data. In the lesson (a video training series by Kevin Skoglund, "PHP: Beyond the Basics",) Kevin's screen looks correct (he's using 5.2.6.)
I'm on 5.3 on my WAMP installation, and my "first_name" attribute of the class Person is not being spit out by the loop... yet the echo property_exists("person","first_name") ? 'true' : 'false';
returns true.
Can anyone help me understand what's going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果属性存在,无论属性和调用者的范围是什么,property_exists 都会返回 true。
get_class_vars
将返回当前范围内可访问的所有属性,以及它们的静态值或默认值(对于未声明静态的属性)。但是,它不会返回类主体中未声明的属性,也不会接受对象参数。请注意,如果使用类名查询未在类主体(即:对象上下文)中声明的属性,property_exists 也将返回 false。
每个例子:
property_exists
will return true if the property exists, no matter what the scope of the property and the caller are.get_class_vars
will return all properties accessible from the current scope, along with their static values, or default values (for properties that are not declared static). However, it will not return properties that are not declared in the class body, nor will it accept an object argument.Note that
property_exists
will also return false if a property that is not declared in the class body (i.e.: object context) is queried using the class name.Per example:
get_class_vars() 仅返回公共访问变量,而 property_exists() 检查公共、受保护和私有变量。
http://php.net/manual/de/function.get-class -vars.php
与
http://php.net/manual/de/function.property-exists.php
get_class_vars() returns only public access variables, while property_exists() checks for public, protected and private ones.
http://php.net/manual/de/function.get-class-vars.php
vs.
http://php.net/manual/de/function.property-exists.php