The private variable in the PHP class is redefined after initialization. The method in the class cannot get the value of the variable. Why?

problem description

defines a class with variable name in A, A. The name variable is defined by the _ _ set () method, and the method tell in A cannot take the value of name. This is why there is no way to go to the value of name
class A {

.
private $name;

public function __set($property,$value){
    $this->property = $value;
}

public function tell(){
    echo $this->name;
}

}

$a = new A ();
$a-> name= "Zhang San";
/ / call the tell () method, but $this- > name has no value
$a-> tell ();

Apr.12,2021

The assignment in

_ _ set () is written incorrectly, which is embarrassing

public function __set($property,$value){
    $this->$property = $value;   //   $this->property = $value;  
}

this you know _ _ set () Why don't you know _ _ get ()

now the code is equivalent to, you set the private property name, but the private property is not accessible outside the class

Menu