Heim  >  Artikel  >  Backend-Entwicklung  >  php: Beispielcode-Sharing zur Zugriffskontrolle (Sichtbarkeit)

php: Beispielcode-Sharing zur Zugriffskontrolle (Sichtbarkeit)

黄舟
黄舟Original
2017-07-02 10:49:381183Durchsuche

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  
  
?>

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  
?>
<?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;));
?>

Die obige Routine gibt Folgendes aus:

Zeichenfolge (5) „Hallo“
Auf die private Methode zugegriffen.

Das obige ist der detaillierte Inhalt vonphp: Beispielcode-Sharing zur Zugriffskontrolle (Sichtbarkeit). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn