Php class method --- magic knowledge drip method, system triggers at a certain timing

  • __get () method get called when a private or protected property
? < PHP
 class Coach 
{ 
    Private $ chairfit = " Xu Xiaodong " ;
     public function __construct () 
    { 
        echo " Welcome to the boxing hall will map ~ training ~ " ; 
    } 
    public function __get ($ chairfit) 
    { 
        echo $ chairfit; // only print attribute name 
    } 
} 
class xxd the extends Coach 
{ 
   public function __construct () 
   { 
       echo " like a man to fight! " . " \ the n- " ;
   } 

} 
$ CJ = new new xxd The (); 
$ CJ -> chairfit;
 >? 
Output:

To fight like a man! # Constructor calls the result
__get chairfit # inherited from the parent class () method



# So you can access the value of the private property of the parent class
? <PHP 
class Coach
{
Private $ chairfit = "Xu Xiaodong";
public function __construct ()
{
echo "Welcome to the boxing hall will map ~ training ~";
}
public function __get ($ chairfit)
{
IF ($ chairfit == ' chairfit ')
{
return $ this-> chairfit;
}
}
}
class the extends xxd The Coach
{
public function the __construct ()
{
echo "\ n-" "like a man to fight!";.
}

}
$ new new CJ CJ = () ;
Print ($ CJ -> chairfit);
?>
Output:

To fight like a man! # Constructor calls the result
Xu Xiaodong # __get by the parent class () method to access the private property of the parent class

 

 

Guess you like

Origin www.cnblogs.com/saintdingspage/p/10958937.html