0

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!!

TravisO
  • 9,406
  • 4
  • 36
  • 44
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
  • 3
    Why are you still using PHP4? It was discontinued three years ago! http://us.php.net/archive/2007.php – Justin Johnson Feb 11 '10 at 19:49
  • That's the most important question. Is this about language theory, or about writing actual new code? – Pekka Feb 11 '10 at 20:10
  • 1
    A huge number of shared hosts still only support PHP 4 its not always feasible to upgrade easily to PHP 5. Legacy still needs maintenance. – Toby Allen Feb 11 '10 at 20:24
  • @Toby Allen .. any site that hasn't upgraded yet are worth leaving, customers need to send a message to crappy hosts. We're not talking about weeks or months, we're talking about YEARS of a discontinued product that's free for them to upgrade. he only barrier is their own laziness, not to mention the speed improvements (means more customers per server) and security fixes. – TravisO Feb 11 '10 at 20:58
  • 1
    @TravisO The primary reason why providers don't upgrade is the fear of a lynch mob burning down the data center when hundreds of PHP4 applications suddenly stop working, or start behaving funny. Still, PHP4 had its time, we all loved it and now it needs to be put to sleep. Legacy applications *really* should be ported because the security risks continuing to run them on an unsupported platform are enormous. See http://stackoverflow.com/questions/1734072/official-end-of-support-for-php4 – Pekka Feb 11 '10 at 21:40
  • Cobal and Fortran Applications are still running 30 years after everyone would have liked to have discontinued support for them. Legacy applications exist and they will continue because generally the cost of upgrading is greater than the cost of doing nothing. – Toby Allen Feb 11 '10 at 22:31

4 Answers4

5

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.

Massimiliano Torromeo
  • 1,801
  • 1
  • 15
  • 17
3

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.

Johnco
  • 4,037
  • 4
  • 34
  • 43
2

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.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

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

GSto
  • 41,512
  • 37
  • 133
  • 184