PHP中如何实现只读成员变量?
当尝试更改它时,抛出异常。
When trying to change it,throw an exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当尝试更改它时,抛出异常。
When trying to change it,throw an exception.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
我想对于类属性,一个解决方案是:
__set
方法,因此在尝试设置该属性时会引发异常。对于变量,我认为不可能有一个只读变量,当您尝试写入它时 PHP 会抛出异常。
例如,考虑这个小类:
实例化该类并尝试读取属性:
将为您提供预期的输出:
在尝试写入属性时:
将为您提供异常:
I suppose a solution, for class properties, would be to :
__get
method to access that property, using the "fake" name__set
method so it throws an exception when trying to set that property.For variables, I don't think it's possible to have a read-only variable for which PHP will throw an exception when you're trying to write to it.
For instance, consider this little class :
Instanciating the class and trying to read the property :
Will get you the expected output :
While trying to write to the property :
Will get you an Exception :
您将其称为
test::CANT_CHANGE_ME
and you refer it as
test::CANT_CHANGE_ME
使用常数。关键字
const
Use a constant. Keyword
const
简而言之,您无法在 PHP 中创建只读对象成员变量。
事实上,大多数面向对象的语言都认为公开公开成员变量是一种糟糕的形式......(C# 是一个巨大而丑陋的例外,其属性构造)。
如果您想要一个类变量,请使用
const
关键字:可以访问该变量:
无论您有多少个
MyClass
类型的不同对象,该变量都将仅存在于一个版本中create,在大多数面向对象的场景中它几乎没有用处。但是,如果您想要一个每个对象可以有不同值的只读变量,则应该使用私有成员变量和访问器方法(也称为 getter):
该变量在构造函数中设置,并将其设置为只读由于没有二传手。但是
MyClass
的每个实例都可以有自己的myVariable
值。The short answer is you can't create a read-only object member variable in PHP.
In fact, most object-oriented languages consider it poor form to expose member variables publicly anyway... (C# being the big, ugly exception with its property-constructs).
If you want a class variable, use the
const
keyword:This variable can be accessed:
This variable will exist in exactly one version regardless of how many different objects of type
MyClass
you create, and in most object-oriented scenarios it has little to no use.If, however, you want a read-only variable that can have different values per object, you should use a private member variable and an accessor method (a k a getter):
The variable is set in the constructor, and it's being made read-only by not having a setter. But each instance of
MyClass
can have its own value formyVariable
.我制作了另一个版本,在文档块中使用
@readonly
而不是private $r_propname
。这仍然不会阻止声明类设置属性,但适用于公共只读访问。示例类:
ReadOnly
特征请参阅源代码&在 我的 gitlab 上进行测试
I made another version that uses
@readonly
in the docblock instead ofprivate $r_propname
. This still doesn't stop the declaring class from setting the property, but will work for public readonly access.Sample Class:
The
ReadOnly
traitSee the source code & test on my gitlab
我也使用一个特征编写了一个版本。
尽管在这种情况下,该属性仍然可以通过其声明类来设置。
声明一个类,如下所示:
这是我所做的特征:
请参阅源代码&在 我的 gitlab 上进行测试
I cooked up a version, too, using a trait.
Though in this case, the property can still be set by its declaring class.
Declare a class like:
And this is the trait I made:
See the source code & test on my gitlab
我知道这是一个老问题,但帕斯卡的回答确实对我有帮助,我想补充一点。
__get() 不仅会在不存在的属性上触发,还会在“无法访问”的属性上触发,例如受保护的属性。这使得创建只读属性变得很容易!
I know this is an old question, but PASCAL's answer really helped me and I wanted to add to it a bit.
__get() fires not only on nonexistent properties, but "inaccessible" ones as well, e.g. protected ones. This makes it easy to make read-only properties!