views:

23

answers:

5

I've been examining the code of CodeIgniter and CakePHP and I noticed that some of the methods in their classes are prefixed with underscore _ / double underscore __.

What's the purpose of that?

+1  A: 

These are Magic Methods in PHP classes:

The function names __construct, __destruct, __call, __callStatic, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __invoke, __set_state and __clone are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.

A method with one underscore has no special meaning. This is more likely some coding convention of the projects.

Felix Kling
+1  A: 

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.

Pekka
+4  A: 

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:

As we cannot use PHP5's private and protected keywords for methods or variables, we agree on following rules:

  • A protected method or variable name start with a single underscore ("_").
  • A private method or variable name start with double underscore ("__").

CodeIgniter conventions:

Methods and variables that are only accessed internally by your class, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.

Gordon
That must be it but I wonder why can't they use PHP5's private and protected keywords?
Emanuil
Interesting side note: Using the double prefix like that goes against PHP's [recommendation](http://www.php.net/manual/en/language.oop5.magic.php): `PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.`
Pekka
@Emanuil because CakePHP wants to be backwards compatible to PHP4. This is one of the major criticisms about CakePHP because they are limiting the framework to the features of a now dead version. The single underscore notation is quite common though even for PHP5 and in combination with visility. See PEAR or ZF coding conventions.
Gordon
Thanks! It all makes sense now.
Emanuil
A: 

I'm not familiar with CakePHP or CodeIgniter, but I think they should be regarded as protected or private for non-CakePHP classes. They are probably public 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.

bouke
A: 

The methods that start with __ are magic methods which get called automatically in php. for more reference , check,

http://php.net/manual/en/language.oop5.magic.php

Yogesh