why uninitialized property is made public in class?

后端 未结 3 1597
囚心锁ツ
囚心锁ツ 2021-01-26 03:28

Consider below code please:

class foo {
    function bar() {
        $this->baz = \'hello there!\';
    }
}

$f = new foo;
$f->bar();
echo $f->baz; // h         


        
相关标签:
3条回答
  • 2021-01-26 03:29

    By default it is set to public.

    Refer to this link: http://www.php.net/manual/en/language.oop5.visibility.php

    0 讨论(0)
  • 2021-01-26 03:36

    It's because how PHP works. If you set uninitialized property, it will be created even without __set. On the other hand, if you try to read uninitialized property, you end with warning.

    0 讨论(0)
  • 2021-01-26 03:43

    Who told that you are not initialized,You have initialized by the statement $this->baz = 'hello there!'; This is how object oriented concept works.You are declaring a class , creating an instance for that class and then accessing that function and at last echoing a variable in that member function,This is how object orientation works...

    0 讨论(0)
提交回复
热议问题