什么是Getter和Setters?

Str*_*rry 4 php oop getter setter

什么是PHP5中的getter和setter?

有人能给我一个很好的解释说明吗?

ant*_*res 8

这是OOP中数据隐藏(或封装)的概念.例如,如果你想在你的类中的某个属性让我们说"量",给你一流的客户端更改或提取其价值你应该让你的变量"金额"私有(不可见对于那些谁使用该选项与在class)并生成两个方法:getter和一个操作你的值的setter(公共).

原因是能够在设置或获取值之前验证数据或对其进行操作.这是一个简短的例子:

class test {

    private $count; //those who use your class are not able to see this property, only the methods above

    public function setCount( $value )    
    {
            //make some validation or manipulation on data here, if needed
        $this->count = $value;    
    }

    public function getCount()    
    {                
        return $this->count;    
    }    
}
Run Code Online (Sandbox Code Playgroud)