7

I've been working with the Joomla framework and I have noticed that they use a convention to designate private or protected methods (they put an underscore "_" in front of the method name), but they do not explicitly declare any methods public, private, or protected. Why is this? Does it have to do with portability? Are the public, private, or protected keywords not available in older versions of PHP?

Steven Oxley
  • 6,563
  • 6
  • 43
  • 55

3 Answers3

17

public, private and protected are PHP5 keywords. unfortunately, PHP4 still has a very high install base (especially amongst shared hosting services).

here's a pretty pic showing july usage rates (text in french). spoiler: php4 still has over a 35% usage rate sadly.

Owen
  • 82,995
  • 21
  • 120
  • 115
  • PHP4 will generate an E_STRICT warning. – Darryl Hein Oct 13 '08 at 04:48
  • E_STRICT was introduced in PHP5, unless i'm misunderstanding what you're saying – Owen Oct 13 '08 at 04:55
  • The opposite was true for a while: the first few PHP 5 releases complained when "var" was used. –  Dec 02 '08 at 01:17
  • 1
    The graph you have linked ends in 2008. On the other hand 4.3 and 4.4 are mentioned separately. Anyway, when you look at a more actual statistic, its quite different: http://w3techs.com/technologies/details/pl-php/all/all Only 10% PHP4 – KingCrunch Nov 28 '10 at 23:56
3

There's some good arguments for not using private methods and properties over here: http://aperiplus.sourceforge.net/visibility.php

Includes: makes coding and reading code harder. Makes re-using classes and debugging harder.

I'm not sold either way, but I would like to see an informed rebuttal of that article, if one exists.

naught101
  • 18,687
  • 19
  • 90
  • 138
  • +1 for a very interesting (and persuasive) link. So, in the last two years, which way did you choose? – akTed Feb 01 '13 at 21:58
  • Heh, in the last two years, I haven't done much PHP coding :P. but I'm generally in favour of giving people enough rope :) – naught101 Feb 03 '13 at 11:02
2

PHP5 introduced some hefty changes in the object model. Among supporting visibility, there are various other changes. Be sure to check out:

PHP 4 classes and objects

PHP 5 classes and objects

Ken
  • 77,016
  • 30
  • 84
  • 101
david
  • 691
  • 1
  • 4
  • 14