关于访问控制的一首PHP面试题(对属性或方法的访问控制)_php技巧

WBOY
Release: 2016-05-17 09:09:04
Original
1255 people have browsed it

复制代码 代码如下:

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


访问控制

对属性或方法的访问控制,是通过在前面添加关键字 public、protected 或 private 来实现的。由 public 所定义的类成员可以在任何地方被访问;由 protected 所定义的类成员则可以被其所在类的子类和父类访问(当然,该成员所在的类也可以访问);而由 private 定义的类成员则只能被其所在类访问。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!