在私有变量的代码之后,公共函数调用没有被执行

时间:2014-03-21 13:09:22

标签: php loops

<?php

/* example for access specifiers*/

class myClass
{
    var $car = "alto"; //if we declare as var it is automatically considered as public
    public $pub = "alphanso"; //even no need to using public keyword  as it is public bydefault
    private $pri = "zen";

    public function myPublic()
    {
        echo "I'm public...can be accessible everywhere";

    }

    private function myPrivate()
    {
        echo "I'm private...no where am accessible,except in current class";

    }

}

$accss = new myClass;

echo $accss->car . "<br>";

echo $accss->pub . "<br>";

echo $accss->pri . "<br>";

$accss->myPublic();

$accss->myPrivate(); //visible only in the class where it is declared.


?>

1 个答案:

答案 0 :(得分:2)

您无法访问private成员或课程外的方法,尝试这样做会引发致命错误。

它无法访问$accss -> myPublic();,因为根据我指定的上述条件,您在echo $accss -> pri."<br>";遇到致命错误。

因此其余的代码将不会被执行。