简体   繁体   English

如何在PHP4中将类属性设为私有?

[英]How to make a class property private in PHP4?

in PHP4 there is no public,private,etc. 在PHP4中没有public,private等。 So I am wondering if there is some sort of work-around so that I can make a class's property private and only accessible via getter/setter 所以我想知道是否存在某种解决方法,以便可以将类的属性设为私有,并且只能通过getter / setter访问

Thanks!! 谢谢!!

You could approach this with a distinct syntax that clearly discourages the usage of such properties. 您可以使用独特的语法来解决此问题,该语法显然不鼓励使用此类属性。 You could borrow the python syntax of starting the method name with an underscore to define it as private. 您可以借用下划线开头的方法名称的python语法将其定义为私有。

This doesn't block anyone from using it, of course, but its usage will be discouraged. 当然,这不会阻止任何人使用它,但不鼓励使用它。

You can't. 你不能 Since the property is itself public, anyone can access it without getter / setter preventing you from actually performing any checks in between. 由于该属性本身是公共的,因此任何人都可以在没有getter / setter的情况下访问它,从而阻止您实际在两者之间进行任何检查。

Can't be done, sorry. 无法完成,对不起。

It's certainly possible to build a workaround by building own getter and setter functions and keeping a table of which property is private/public/protected, but it's hardly worth the effort anymore, is it? 通过构建自己的getter和setter函数并保留一个私有或公共/受保护的属性表来构建变通方案肯定是可行的,但现在再也不值得为此付出努力了,不是吗? PHP 4 is really outdated, see here . PHP 4确实过时了,请参阅此处

您仍然可以为该变量编写getter和setter,但是在PHP 4中无法将函数设为私有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM