PHP access control: public/protected/private

伊谢尔伦
Release: 2016-11-23 14:16:42
Original
1149 people have browsed it

Access control of properties or methods is achieved by adding the keywords public, protected or private in front. Class members defined as public can be accessed from anywhere. Class members defined as protected can be accessed by itself and its subclasses and parent classes. Class members defined as private can only be accessed by the class in which they are defined.

1. Attribute access control

Class attributes must be defined as one of public, protected, and private. If defined with var, it is considered public.
Example #1 Property declaration

class MyClass
{ 
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';
    function printHello()
    {
        echo $this->public.&#39;<br>&#39;;
        echo $this->protected.&#39;<br>&#39;;
        echo $this->private.&#39;<br>&#39;;
    }
}
$obj = new MyClass();
echo $obj->public; //这行可以正常运行
echo $obj->protected; //这行会产生一个致命错误
echo $obj->private; //这行也会产生一个致命错误
$obj->printHello(); //正常输出public、protected、private的值
class MyClass2 extends MyClass
{
    protected $protected = &#39;Protected2&#39;;
    function printHello(){
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
$obj2 = new MyClass2();
echo $obj2->public; //这行可以正常执行
echo $obj2->private; //未定义private
echo $obj2->protected; //产生一个致命的错误
$obj2->printHello(); //输出Public、Protected和Undefined
Copy after login

Note: For compatibility reasons, the method of using the var keyword to define variables in PHP 4 is still valid in PHP 5 (just as an alias of the public keyword). In versions prior to PHP 5.1.3, this syntax produced an E_STRICT warning.

Method Access Control

Methods in a class can be defined as public, private or protected. If these keywords are not set, the method defaults to public.
Example #2 Method declaration

class MyClass
{
    //声明一个公有的构造函数
    public function __construct(){}
    //声明一个公有的方法
    public function MyPublic(){}
    //声明一个受保护的方法
    protected function MyProtected(){}
    //声明一个私有的方法
    private function MyPrivate(){}
    //此方法为公有
    function Foo()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass -> MyPublic(); //这行能被正常执行
$myclass -> MyProtected(); //这行会产生一个致命错误
$myclass -> MyPrivate(); //这行会产生一个致命错误
$myclass -> Foo(); //公有,受保护,私有都可以执行
class MyClass2 extends MyClass
{
    //此方法为公有
    function Foo2()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate(); //这行会产生一个致命错误
    }
}
$myclass2 = new MyClass2;
$myclass2 -> MyPublic(); //这行能被正常执行
$myclass2 -> Foo2(); //公有和受保护的都可以执行,但私有的不行
class Bar
{
    public function test(){
        $this->testPrivate();
        $this -> testPublic();
    }
    public function testPublic(){
        echo "Bar::testPublic<br>";
    }
    private function testPrivate(){
       echo "Bar::testPrivate<br>";
    }
}
class Foo extends Bar
{
    public function testPublic(){
        echo "Foo::testPublic<br>";
    }
    public function testPrivate(){
       echo "Foo::testPrivate<br>";
    }
}
$myFoo = new Foo();
$myFoo -> test(); //Bar::testPrivate
//Foo::testPublic
Copy after login

Access control of other objects

Objects of the same class can access each other's private and protected members even if they are not the same instance. This is because the internal implementation details of these objects are known.
Example #3 Access private members of the same object type

class Test
{
    private $foo;
    public function __construct($foo)
    {
        $this->foo = $foo;
    }
    private function bar()
    {
        echo "Accessed the private method."; 
    }
    public function baz(Test $other)
    {
        //我们可以在这里改变私有属性的值
        $other->foo = &#39;hello&#39;;
        var_dump($this->foo);
        //我们也可以在这里调用私有的方法
        $other->bar();
    }
}
$test = new Test(&#39;test&#39;);
$test -> baz(new Test(&#39;other&#39;));
Copy after login

Output result:

string(4) "test"
Accessed the private method.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!