Underline Method Prefix

I studied CodeIgniter and CakePHP code, and I noticed that some of the methods in their classes are prefixed with an underscore _ or double underscore __ .

What is the purpose of this?

+4
source share
7 answers

In case it is not PHP magic methods , it should indicate Visibility in the absence of the right visibility keywords:

Cake Encoding Conventions:

Since we cannot use PHP5 private and protected keywords for methods or variables, we agree to the following rules:

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

CodeIgniter conventions :

Methods and variables that are only available inside your class, such as utility and helper functions that your public methods use to abstract code, must have an underscore prefix.

+11
source

These are the 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 magic in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functions associated with them.

The underlined method alone does not really matter. This is most likely an agreement on coding projects.

+1
source

They are probably magic methods . There are several such methods that fulfill a specific purpose (object constructor, object destructor, getter, setter ...)

PHP reserves the __ prefix in function names for these magic functions. He recommended not defining functions with this prefix for any other purpose.

Update: Both frameworks use the __ prefix for their own purposes. See @ Gordon's answer.

+1
source

In Codeigniter, methods inside controllers can usually be called part of the URL, so you can call the "index" method in the "main" of the controller as follows:

mysite.com/main/index.

You can also have a method inside your controller that you do not want anyone to call a segment in the URL, so you would prefix it with "_" (one underscore) .. this is different than making it private. Private methods can only be called in the class where it is defined. Thus, the controller method can have an underscore prefix that would make it unimaginable as a URL segment, and it could also be declared private, which would make it unsuitable for other classes.

+1
source

I am not familiar with CakePHP or CodeIgniter, but I think they should be considered protected or private for classes other than CakePHP. They are probably public , because they can be called from other classes, which makes some kind of hack. Note that in PHP there are __get , __construct and other magic methods (as mentioned above).

0
source

Methods starting with __ are magic methods that are automatically called in php. for more information check

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

0
source

I have a precedent for this, which is my own preference. I will often write traits designed to adhere to a particular interface, although since traits cannot directly implement an interface, I will indicate which interface it satisfies in the comment block of the dock code, as well as the prefix of protected and private methods that are not associated with an interface with one underline. This allows you to pretty easily keep track of which methods are provided to satisfy the contract (interface) and which methods support. For instance:

 interface Foo { public function bar(array $args = null, array $flags = null); } 

The sign below the goal should satisfy the requirements of the Foo interface, but only one of its methods is required for this. For clarity, protected methods are prefixed. Even if they become publicly available later, this still indicates that they are interface-independent and should not be considered relevant.

 /** * @satifies Foo */ trait FooTrait { public function bar(array $args = null, array $flags = null) { $this->_handleArgs($args); $this->_handleFlags($flags); } protected function _handleArgs(array $args = null) { if (is_null($args) { return; } //do something with the args } protected function _handleFlags(array $flags = null) { if (is_null($flags) { return; } //do something with the flags } } 

Then you can satisfy the interface by implementing it in the class and using the corresponding attribute without additional work.

 final class ConcreteFoo implements Foo { use FooTrait; } 

This greatly simplifies communication and allows you to easily integrate with other classes from other libraries or frameworks that require an inheritance chain, without having to collapse your logic using the adapter class group.


My IDE (Netbeans) grumbles about this as a violation of PSR-1. Since PSR-1 does not directly affect execution, and it can be argued that this is a more readable approach or not, I could take care of it less. I am trying to execute all PSRs that directly affect the execution.

0
source

All Articles