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?
protected - the property or method can be accessed within the class and by classes derived from that class. private - the property or method can ONLY be accessed within the class.
It depends on which type of application your are trying to develop. All three types are secure if they are used in the right way. If you are going to use the functions everywhere in the program you should use public.
$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object.
The constructor may be made private or protected to prevent it from being called externally. If so, only a static method will be able to instantiate the class. Because they are in the same class definition they have access to private methods, even if not of the same object instance.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With