I've been examining the code of CodeIgniter and CakePHP and I noticed that some of the methods in their classes are prefixed with an underscore _
or a double underscore __
.
What's the purpose of that?
I've been examining the code of CodeIgniter and CakePHP and I noticed that some of the methods in their classes are prefixed with an underscore _
or a double underscore __
.
What's the purpose of that?
In the case where it is not any of PHP's magic methods, it is to indicate Visibility in lack of proper Visibility keywords:
Cake Coding Conventions:
CodeIgniter conventions:
These are Magic Methods in PHP classes:
A method with one underscore has no special meaning. This is more likely some coding convention of the projects.
They are probably magic methods. There is a number of those methods that serve a specific purpose (object constructor, object destructor, getter, setter...)
PHP reserves the
__
prefix in function names for those magical functions. It's recommended not to define functions with that prefix for any other purpose.Update: Both frameworks seem to use the
__
prefix for their own purposes as well. See @Gordon's answer.I have a use case for this that is my own preference. I will often write traits that are intended to honor a specific interface, although since traits cannot directly implement an interface, I will designate which interface the trait satisfies in the doc block comment, and prefix protected and private methods that are not related to the interface with a single underscore. This lets you pretty easily follow which methods are provided to satisfy the contract (interface), and which are supporting methods. Eg:
The trait below's purpose is to satisfy the requirements of the
Foo
interface, but only one of it's methods are needed to do so. For clarity, the protected methods are prefixed. Even if they are made public by extension later, this still indicates that they are not contractually dependent by interface, and should not be assumed to be anything relevant.You can then satisfy the interface by implementing it on a class and using the corresponding trait with no additional work.
This keeps things very loosely coupled, and makes it possible to pretty easily integrate with other classes from other libraries or frameworks that do require a chain of inheritance without having to convolute your logic with a bunch of adapter classes.
My IDE (Netbeans) grumbles about this as a violation of PSR-1. As PSR-1 does not directly affect execution and it is arguable as to whether this is a more readable approach or not, I could really care less. I do try to follow all of the PSR's that directly affect execution though.
In Codeigniter, methods within controllers can normally be called as part of the url so you might call the method "index" in controller "main" as follows:
mysite.com/main/index.
You might also have a method within your controller that you don't want someone to be able to call as a segment in the url, so you would prefix it with a "_" (single underscore) .. that is different than making it private. Private methods are only callable within the class where it is defined. So a controller method could be prefixed with an underscore which would make it uncallable as a url segment and it could also be declared private which would make it uncallable from other classes.
I'm not familiar with CakePHP or CodeIgniter, but I think they should be regarded as
protected
orprivate
for non-CakePHP classes. They are probablypublic
as they can be called from other classes, making some kind of hack. Please note that__get
,__construct
and other magic methods (as noted above) exists in PHP.