Home > Backend Development > PHP Tutorial > php: Access control for members in a class

php: Access control for members in a class

黄舟
Release: 2023-03-12 11:16:01
Original
2675 people have browsed it

Access control

Access control on properties or methods is achieved by adding the keywords public, protected or private in front.

Class members defined by public can be accessed anywhere;

Class members defined by protected can be accessed by subclasses and parent classes of the class in which they are located (of course, the The class in which the member is located can also be accessed); class members defined by private can only be accessed by the class in which they are located.

The following three access modifiers are supported in PHP5.

(1)public. This modifier is the default, if no access modifier is specified for the property or method, it

is public. Public properties or methods can be accessed both inside and outside the class.

(2)private. This modifier indicates that the marked property or method can only be accessed within the class. Cannot be inherited!

(3)protected. This modifier indicates that the marked property or method can only be accessed within the class. Can be inherited

private The same class can be accessed, but neither subclasses nor external classes can access it;

protected The same class and subclasses can be accessed, but external classes cannot.

Access control of class members

Class members must be defined using the keywords public, protected or private

<?php
class MyClass
{
    public $public = &#39;Public&#39;;
    protected $protected = &#39;Protected&#39;;
    private $private = &#39;Private&#39;;
    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
$obj = new MyClass();
echo $obj->public; // 这行能被正常执行
echo $obj->protected; // 这行会产生一个致命错误
echo $obj->private; // 这行也会产生一个致命错误
$obj->printHello(); // 输出 Public、Protected 和 Private
class MyClass2 extends MyClass
{
    // 可以对 public 和 protected 进行重定义,但 private 而不能
    protected $protected = &#39;Protected2&#39;;
    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
$obj2 = new MyClass2();
echo $obj->public; // 这行能被正常执行
echo $obj2->private; // 未定义 private
echo $obj2->protected; // 这行会产生一个致命错误
$obj2->printHello(); // 输出 Public、Protected2,但不会输出 Private
class Bar 
{
    public function test() {
        $this->testPrivate();
        $this->testPublic();
    }
    public function testPublic() {
        echo "Bar::testPublic\n";
    }
    
    private function testPrivate() {
        echo "Bar::testPrivate\n";
    }
}
class Foo extends Bar 
{
    public function testPublic() {
        echo "Foo::testPublic\n";
    }
    
    private function testPrivate() {
        echo "Foo::testPrivate\n";
    }
}
$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
Copy after login

Access control of methods

Methods in a class must be defined using the keywords public, protected or private. If these keywords are not set, the method will be set to the default public.

<?php

class MyClass
{
    // 构造函数必须是 public
    public function construct() { }

    // 声明一个 public 的方法
    public function MyPublic() { }

    // 声明一个 protected 的方法
    protected function MyProtected() { }

    // 声明一个 private 的方法
    private function MyPrivate() { }

    // 这个方法也是 public 的
    function Foo()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}

$myclass = new MyClass;
$myclass->MyPublic(); // 这行能被正常执行
$myclass->MyProtected(); // 这行会产生一个致命错误
$myclass->MyPrivate(); // 这行会产生一个致命错误
$myclass->Foo(); // Public、Protected 和 Private 都被调用了



class MyClass2 extends MyClass
{
    // This is public
    function Foo2()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate(); // 这行会产生一个致命错误
    }
}

$myclass2 = new MyClass2;
$myclass2->MyPublic(); // 这行能被正常执行
$myclass2->Foo2(); // Public 和 Protected 都被调用了,但 Private 不会被调用
?>
Copy after login

The above is the detailed content of php: Access control for members in a class. For more information, please follow other related articles on the PHP Chinese website!

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