php 本身没有类似C# readonly这样的修饰符,应该要通过设计实现了吧
本文来源gaodai#ma#com搞*!代#%^码$网!搞代gaodaima码
回复内容:
php 本身没有类似C# readonly这样的修饰符,应该要通过设计实现了吧
常量不就行了吗?
define(key,value)
Talk is cheap,i will show you the code demo.
Like this:
<code>//1.first snippetclass HelloSomeOne{ const NAME = "PHP技术大全"; //todo something else.}//2. second snippet//not in class body innerconst NAME = "PHP技术大全";</code>
php没有提供这样的功能,不过在面向对象的设计中,可以通过set/get方法实现。
<code>class { private $a = null; public function setA($a) { if (null === $this->a) { $this->a = $a; } } public function getA() { return $this->a; }}</code>
对于set/get方法,可以用__set/__get这两个魔术函数实现,书写效果可以更佳。
在PHP脚本里可以用define实现,在PHP类里可以用const实现