get_class_vars() 未显示变量,但在同一类上运行的 property_exists() 返回 true

发布于 2024-10-18 00:26:15 字数 1282 浏览 0 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野鹿林 2024-10-25 00:26:15

如果属性存在,无论属性和调用者的范围是什么,property_exists 都会返回 true。

get_class_vars 将返回当前范围内可访问的所有属性,以及它们的静态值或默认值(对于未声明静态的属性)。但是,它不会返回类主体中未声明的属性,也不会接受对象参数。

请注意,如果使用类名查询未在类主体(即:对象上下文)中声明的属性,property_exists 也将返回 false。

每个例子:

class Foo {
    public $foo;
    private $bar;

    public function test() {
        var_dump(get_class_vars(__CLASS__));
    }
}

$obj = new Foo;
$obj->baz = 'hello';
property_exists($obj, 'bar');            // true
property_exists($obj, 'baz');            // true
property_exists(get_class($obj), 'baz'); // false

get_class_vars(get_class($obj));         // you get "foo" only
$obj->test();                            // you get "foo" and "bar", not "baz"

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:

class Foo {
    public $foo;
    private $bar;

    public function test() {
        var_dump(get_class_vars(__CLASS__));
    }
}

$obj = new Foo;
$obj->baz = 'hello';
property_exists($obj, 'bar');            // true
property_exists($obj, 'baz');            // true
property_exists(get_class($obj), 'baz'); // false

get_class_vars(get_class($obj));         // you get "foo" only
$obj->test();                            // you get "foo" and "bar", not "baz"
莳間冲淡了誓言ζ 2024-10-25 00:26:15

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文