Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not use 'protected' or 'private' in PHP?

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?

like image 704
Steven Oxley Avatar asked Oct 13 '08 04:10

Steven Oxley


People also ask

What is difference between private and protected in 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.

Which is more secure private or protected?

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.

Why we use $this in PHP?

$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.

What happen if constructor is defined as private or protected in PHP?

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.


1 Answers

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.

like image 179
Owen Avatar answered Nov 10 '22 01:11

Owen