How to make a class property private in PHP4?

人走茶凉 提交于 2019-12-13 06:47:21

问题


in PHP4 there is no public,private,etc. 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

Thanks!!


回答1:


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.

This doesn't block anyone from using it, of course, but its usage will be discouraged.




回答2:


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.




回答3:


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? PHP 4 is really outdated, see here.




回答4:


you can still write getters and setters for that variable, but there is no way to make a function private in PHP 4.



来源:https://stackoverflow.com/questions/2247092/how-to-make-a-class-property-private-in-php4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!