<?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.
?>
答案 0 :(得分:2)
您无法访问private
成员或课程外的方法,尝试这样做会引发致命错误。
它无法访问$accss -> myPublic();
,因为根据我指定的上述条件,您在echo $accss -> pri."<br>";
遇到致命错误。
因此其余的代码将不会被执行。