php access control visibility public protected private

Access control to properties or methods is achieved by adding the keywords public (public), protected (protected), and private (private) in front.

Class members defined as public can be accessed anywhere.

A class member defined as protected is accessed by itself and its subclasses and superclasses.

A class member defined as private can only be accessed by the class in which it is defined

 

attribute access control

Class properties must be defined as public, protected, or private. If var is used, it is considered public.

<?php
/**
 * Define MyClass
 */
class MyClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new MyClass();
 echo  $obj -> public ; // this line can be executed normally 
echo  $obj -> protected ; // this line will generate a fatal error 
echo  $obj -> private ; // this line Will also generate a fatal error 
$obj ->printHello(); // Output Public, Protected and Private


/**
 * Define MyClass2
 */
class MyClass2 extends MyClass
{
    // Can redefine public and protected, but private and not 
    protected  $protected = 'Protected2' ;

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj2 = new MyClass2();
 echo  $obj2 -> public ; // this line can be executed normally 
echo  $obj2 -> private ; // undefined private 
echo  $obj2 -> protected ; // this line will generate a fatal Error 
$obj2 ->printHello(); // Output Public, Protected2 and Undefined

?>

Note: For compatibility reasons, the method of using the var keyword to define variables in PHP4 is still valid in php5 (just as an alias for the public keyword). In versions prior to php5.1.3, this syntax would produce a E_STRICT warning.

 

method access control

Methods in a class can be defined as public, private or protected. If these keywords are not set, the method defaults to public

 

<?php
/**
 * Define MyClass
 */
class MyClass
{
    // declare a public constructor 
    public  function __construct() { }

    // declare a public method 
    public  function MyPublic() { }

    // declare a protected method 
    protected  function MyProtected() { }

    // declare a private method 
    private  function MyPrivate() { }

    // This method is public 
    function Foo()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}

$myclass = new MyClass;
 $myclass ->MyPublic(); // this line can be executed normally 
$myclass ->MyProtected(); // this line will generate a fatal error 
$myclass ->MyPrivate(); // this The line will generate a fatal error 
$myclass ->Foo(); // public, protected, private can be executed


/**
 * Define MyClass2
 */
class MyClass2 extends MyClass
{
    // This method is public 
    function Foo2()
    {
        $this - > MyPublic() ;
         $this -> MyProtected();
         $this ->MyPrivate(); // This line will generate a fatal error 
    }
}

$myclass2 = new MyClass2;
 $myclass2 ->MyPublic(); // this line can be executed normally 
$myclass2 ->Foo2(); // both public and protected are executable, but not 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 
?>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325156935&siteId=291194637