An PHP interview question about access control: of's access control over properties or methods

  • 2020-05-24 05:16:13
  • OfStack

 
class Foo 
{ 
private $name = 'hdj'; 
public function getName(){ 
return $this->name; 
} 
} 
class Bar extends Foo 
{ 
public $name = 'deeka'; 
} 
$bar = new Bar; 
var_dump($bar->name); 
var_dump($bar->getName()); 


Access control

Access control over properties or methods is achieved by adding the keywords public, protected, or private to the front. Class members defined by public can be accessed anywhere; A class member defined by protected can be accessed by its subclass and superclass (and, of course, by the class in which the member resides). Class members defined by private can only be accessed by their own classes.


Related articles: