php 本身没有类似c# readonly这样的修饰符,应该要通过设计实现了吧
php 本身没有类似c# readonly这样的修饰符,应该要通过设计实现了吧
常量不就行了吗?
define(key,value)
Talk is cheap,i will show you the code demo.
Like this:
<code>//1.first snippet
class HelloSomeOne{
const NAME = "PHP技术大全";
//todo something else.
}
//2. second snippet
//not in class body inner
const NAME = "PHP技术大全";</code>
php没有提供这样的功能,不过在面向对象的设计中,可以通过set/get方法实现。
立即学习“PHP免费学习笔记(深入)”;
<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实现
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号