Home > php教程 > php手册 > body text

PHP面试题之访问控制

WBOY
Release: 2016-06-13 10:15:44
Original
1196 people have browsed it

对属性或方法的访问控制,是通过在前面添加关键字 public、protected 或 private 来实现的。由 public 所定义的类成员可以在任何地方被访问;由 protected 所定义的类成员则可以被其所在类的子类和父类访问(当然,该成员所在的类也可以访问);而由 private 定义的类成员则只能被其所在类访问

 代码如下 复制代码
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());
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template