Home >Backend Development >PHP Tutorial >PHP object-oriented syntax 4: access modification qualifier, public, protected, private

PHP object-oriented syntax 4: access modification qualifier, public, protected, private

黄舟
黄舟Original
2016-12-28 10:15:021778browse

Access modification qualifier:
Used to describe where a member (property, method) can be accessed!

tip:
PHP uses the concept of classes to restrict access to members!
PHP divides the accessed code into three major areas: within the class, outside the class, and within the inheritance chain class!

is based on:
Where the target member is defined and where the target member is accessed (see the current access->where the code of the attribute/method is executed) determines whether it is within the class, outside the class, or inherited. Within the chain category!

<?php 
class A{
    public function iam_a(){
            var_dump($this->property);
    } 
}
class B extends A{
    //在B类中定义
    public $property=&#39;phpphphp&#39;;
    public function iam_b(){
        var_dump($this->property);
    }
}
class C extends B{
    public function iam_c(){
            var_dump($this->property);
    }
}
/*public $property=&#39;phpphphp&#39;;公共的,指的是,成员在本类内,继承链上的类内,与类外,都可以被访问!*/
//明确目标成员$property是在B类中定义的。
$c=new C;
// $c->property;在B类外执行,可以访问到
echo $c->property;
echo &#39;<hr/>&#39;;
// iam_c()中的$this->property是在B类的继承链内,可以访问到
$c->iam_c();
// iam_b()中的$this->property是在B类内,可以访问到
$c->iam_b();
//iam_a()中的$this->property是在B类的继承链内,可以访问到
$c->iam_a();
运行结果:
string(8) "phpphphp"
string(8) "phpphphp"
string(8) "phpphphp"
string(8) "phpphphp"

protected $property='phpphphp';
Protected means that members can be accessed within this class and within classes on the inheritance chain (subclass, parent class)

$c=new C;
// $c->property;在B类外执行,不能访问到
var_dump($c->property);
// iam_c()中的$this->property是在B类的继承链内,可以访问到
$c->iam_c();
// iam_b()中的$this->property是在B类内,可以访问到
$c->iam_b();
//iam_a()中的$this->property是在B类的继承链内,可以访问到
$c->iam_a();
运行结果:
PHP Fatal error: Cannot access protected property C::$property in /usercode/file.php on line 25

private $property='phpphphp'
Private, means that only within this class can access!

$c=new C;
// $c->property;在B类外执行,不能访问到
var_dump($c->property);
// iam_c()中的$this->property是在B类的继承链内,不能访问到
$c->iam_c();
// iam_b()中的$this->property是在B类内,可以访问到
$c->iam_b();
//iam_a()中的$this->property是在B类的继承链内,不能访问到
$c->iam_a();
运行结果:
NULL
NULL
string(8) "phpphphp"
NULL
PHP Notice: Undefined property: C::$property in /usercode/file.php on line 25 
PHP Notice: Undefined property: C::$property in /usercode/file.php on line 19 
PHP Notice: Undefined property: C::$property in /usercode/file.php on line 4

How to choose:
One principle is to try to reflect the encapsulation. Encapsulation means hiding the internal implementation as much as possible and only developing external operation interfaces! Syntactically, that is, privatize (protect) the properties and methods that are not needed for external use, leaving only some necessary public methods!
A class has at least one public method that can be called outside the class.
If a class is not intended to be inherited, members can be private. If a class is used as a base class and is designed to be inherited by subclasses, some members will be protected. It still depends on the specific business requirements.

tip:
1. Generally, if rewriting occurs, the definition position after rewriting shall prevail.
(The exception is that private properties cannot be overridden when accessed within this class.)

<?php 
class A{
    public function iam_a(){
            var_dump($this->property);
    } 
}
class B extends A{
    //在B类中定义public $property
    public $property=&#39;phpphphp&#39;;
    public function iam_b(){
            var_dump($this->property);
    }
}
class C extends B{
//C类定义了同名属性
    public $property=&#39;chongxiechongxiechongxie&#39;;
    public function iam_c(){
            var_dump($this->property);
    }
}
$c=new C;
echo $c->property;
echo &#39;<hr/>&#39;;
//确定了下面三个方法中$this都是C类对象,先查找C类中有没有定义property属性,如果没有,再往父类找。
$c->iam_c();
$c->iam_b();
$c->iam_a();
运行结果:
chongxiechongxiechongxie
string(24) "chongxiechongxiechongxie"
string(24) "chongxiechongxiechongxie"
string(24) "chongxiechongxiechongxie"

Note: When overriding properties or methods of the parent class, the access control modifier must be higher than the same name of the parent class. The property or method is weak. Private is the strongest and public is the weakest.

Exception:
Private properties cannot be overridden when accessed within this class.

<?php 
class A{
    public function iam_a(){
            echo &#39;在继承链内访问<br/>&#39;;
            var_dump($this);
            var_dump($this->property);
            echo &#39;<hr/>&#39;;
    } 
}
class B extends A{
    //在B类中定义private $property
    private $property=&#39;phpphphp&#39;;
    public function iam_b(){
        echo &#39;类内访问<br/>&#39;;
            var_dump($this);
            var_dump($this->property);
            echo &#39;<hr/>&#39;;
    }
}
class C extends B{
//在C类中定义同名属性
    public $property=&#39;chongxiechongxiechongxie&#39;;
    public function iam_c(){
            echo &#39;在继承链内访问<br/>&#39;;
            var_dump($this);
            var_dump($this->property);
            echo &#39;<hr/>&#39;;
    }
}
$c=new C;
echo &#39;在类外访问&#39;.$c->property;//在类外访问
echo &#39;<hr/>&#39;;
$c->iam_c();//在继承链内访问
$c->iam_b();//在B类内访问
$c->iam_a();//在继承链内访问
运行结果:
在类外访问chongxiechongxiechongxie
在继承链内访问string(24) "chongxiechongxiechongxie"
object(C)#1 (2) {
["property"]=>
string(24) "chongxiechongxiechongxie"
["property":"B":private]=>
string(8) "phpphphp"
}
string(24) "chongxiechongxiechongxie"
类内访问,不能被重写,string(8) "phpphphp"
如果有知道原因的同学,请留个言解答下,谢谢
object(C)#1 (2) {
["property"]=>
string(24) "chongxiechongxiechongxie"
["property":"B":private]=>
string(8) "phpphphp"
}
string(8) "phpphphp"
在继承链内访问string(24) "chongxiechongxiechongxie"
object(C)#1 (2) {
["property"]=>
string(24) "chongxiechongxiechongxie"
["property":"B":private]=>
string(8) "phpphphp"
}
string(24) "chongxiechongxiechongxie"

The above is the PHP object-oriented syntax 4: access modification qualifier, public, protected, private content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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