Home > Backend Development > PHP Tutorial > php: Sample code sharing about access control (visibility)

php: Sample code sharing about access control (visibility)

黄舟
Release: 2023-03-12 11:10:02
Original
1262 people have browsed it

Php code

<?php  
/** 
 * Define MyClass 
 */  
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  
  
  
/** 
 * Define MyClass2 
 */  
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 $obj2->public; // 这行能被正常执行  
echo $obj2->private; // 未定义 private  
echo $obj2->protected; // 这行会产生一个致命错误  
$obj2->printHello(); // 输出 Public、Protected2 和 Undefined  
  
?>
Copy after login

Php code

<?php  
/** 
 * Define MyClass 
 */  
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(); // 公有,受保护,私有都可以执行  
  
  
/** 
 * Define MyClass2 
 */  
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\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
<?php
class Test
{
    private $foo;

    public function construct($foo)
    {
        $this->foo = $foo;
    }

    private function bar()
    {
        echo &#39;Accessed the private method.&#39;;
    }

    public function baz(Test $other)
    {
        // We can change the private property:
        $other->foo = &#39;hello&#39;;
        var_dump($other->foo);

        // We can also call the private method:
        $other->bar();
    }
}

$test = new Test(&#39;test&#39;);

$test->baz(new Test(&#39;other&#39;));
?>
Copy after login

The above routine will output:

string(5) " hello"
Accessed the private method.

The above is the detailed content of php: Sample code sharing about access control (visibility). 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