Home > Backend Development > PHP Tutorial > Introduction to access control of attributes or methods in PHP (code example)

Introduction to access control of attributes or methods in PHP (code example)

不言
Release: 2023-04-04 10:54:01
forward
2043 people have browsed it

This article brings you an introduction to access control of attributes or methods in PHP (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Access control of properties or methods in PHP is by adding the keywords public (public), protected (protected) or Private (private) to achieve. Class members defined as public can be accessed from anywhere, and 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.

Class attributes must be defined as public, protected or private. If defined with var, it is considered public. Subclasses can modify the values ​​of public and protected properties of the base class.

<?php
class A{
    var $a="a";
    protected $b="b";
    private $c="c";
    
    function printVar(){
        echo $this->a.";".$this->b.";".$this->c.";"."\n";
    }
}
class B extends A{
    public $a="aa";
    protected $b="bb";
    private $c="cc";
}
$a=new A();
$a->printVar();
$b=new B();
$b->printVar();
?>
Copy after login

Classes must be defined as public, protected or private. If not specified, it is considered public. Subclasses can override public and protected functions of the base class.

<?php
class A{
    public function fa(){
        echo "A->fa\n";
    }
    protected function fb(){
        echo "A->fb\n";
    }
    private function fc(){
        echo "A->fc\n";
    }
    function showFoo(){
        $this->fa();
        $this->fb();
        $this->fc();
    }
}
class B extends A{
    function fa(){
        echo "B->fa\n";
    }
    protected function fb(){
        echo "B->fb\n";
    }
    private function fc(){
        echo "B->fc\n";
    }
}
$a=new A();
$a->showFoo();
$b=new B();
$b->showFoo();
?>
Copy after login

Objects of the same class, even if they are not the same instance, can access each other's private and protected members, because the internal implementation details of these objects are known.

<?php
class A
{
    private $a;

    public function __construct($a)
    {
        $this->a = $a;
    }

    private function fa()
    {
        echo &#39;private method fa.&#39;;
    }

    public function foo(A $other,$v)
    {
        $other->a = $v;
        var_dump($other->a);
        $other->fa();
    }
}

$a = new A(&#39;a&#39;);
$aa=new A(&#39;aa&#39;);
$a->foo($aa,&#39;newA&#39;);
?>
Copy after login

The above is the detailed content of Introduction to access control of attributes or methods in PHP (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
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