php 本身没有类似c# readonly这样的修饰符,应该要通过设计实现了吧
回复内容: php 本身没有类似c# readonly这样的修饰符,应该要通过设计实现了吧
常量不就行了吗?
define(key,value)
talk is cheap,i will show you the code demo.
like this:
//1.first snippetclass hellosomeone{ const name = php技术大全; //todo something else.}//2. second snippet//not in class body innerconst name = php技术大全;
php没有提供这样的功能,不过在面向对象的设计中,可以通过set/get方法实现。
class { private $a = null; public function seta($a) { if (null === $this->a) { $this->a = $a; } } public function geta() { return $this->a; }}
对于set/get方法,可以用__set/__get这两个魔术函数实现,书写效果可以更佳。
在php脚本里可以用define实现,在php类里可以用const实现