Get_class_vars () does not show the variable, but the ex_xists () property, running in the same class, returns true

I am learning PHP and I started playing with classes - below, perhaps 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 is done, it should output a data bit. In the lesson ( Kevin Skoglund video training series , PHP: Beyond Basics, ") Kevin’s screen looks right (it uses 5.2.6.)

I am in 5.3 on my WAMP installation, and my Person attribute "first_name" does not spit out in the loop ... but echo property_exists("person","first_name") ? 'true' : 'false';returns true.

Can someone help me understand what is going wrong?

+3
source share
2 answers

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"
+4

Source: https://habr.com/ru/post/1792386/


All Articles