Welcome Guest, Not a member yet? Register   Sign In
Controller Method Name Prefix
#1

The CI 3.10 documentation indicates that "Prefixing method names with an underscore will also prevent them from being called."
However, PHP has no such naming restriction. Is this restriction something that will be removed in a future CI release?
Reply
#2

(10-14-2016, 08:14 AM)Cannondale Wrote: The CI 3.10 documentation indicates that "Prefixing method names with an underscore will also prevent them from being called."
However, PHP has no such naming restriction. Is this restriction something that will be removed in a future CI release?

You can also make the function private.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#3

I wonder if the underscore convention might have been devised because prior to PHP 5.0 visibility for class properties and methods did not exist. In other words, the keywords "public", "protected", and "private" were not available for classes and objects.

The original release of CI was in 2006 and I imagine that a lot of sites were still using PHP version 4 at the time.
Reply
#4

dave friend,

You make an interesting observation. In my company, we have php standards that require custom functions/methods to start with an underscore in order to quickly and easily distinguish between native and custom code. At the very least, it seems that CI should make this an option in the config file so that ALL of our php code including CI code would comply with current php naming standards and any internal shop coding standards.
Reply
#5

i looked into this and there are many other frameworks that use the convention of underscores to denote private methods. for some it was required. its relatively recently that its been considered a "bad practice". I've gone back and forth but the one thing i really like about it is when calling a private method in a controller

PHP Code:
$this->_verifyUser($id); 

$this->verifyUser($id); 

in codeigniter, with the first one you know immediately you are calling a private method.
Reply
#6

cartalot,

Thanks for the additional information. I do understand that a underscore prefix previously denoted a private method to the reader but was NOT enforced by php. However, I believe the use of this convention is old and has faded. As mentioned in my original post, php does not restrict using an underscore prefix in a function, method or class name. Per the php documentation:

A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

If php does not restrict using an underscore prefix in a function, method or class name, why should CI???
Reply
#7

Quote:If php does not restrict using an underscore prefix in a function, method or class name, why should CI???

I'm sticking with the "because of PHP 4.x.x" explanation. Back in the day, before visibility modifiers existed in PHP, a scheme had to be devised to prevent controller methods not intended to be called by way of a URL from well... being called by a URL.

It's probably safe to assume that the convention continues in order to maintain backward compatibility. And for the same reason I doubt it will be removed from CI 3.x.x CI version 4 is a different matter and someone more in the know than I will have to address that topic.

The leading underscore is handy for readability reasons as others have pointed out. It's a form of "hungarian notation" after a fashion. That particular naming convention has fallen out of style probably because of the prevalence of loosely typed programming languages - i.e. PHP.
Reply
#8

Indeed, this comes from before visibility declarations got into PHP.
If you search the forums, you'd probably find another thread where I've confirmed this.
Reply
#9

I think this should be kept.

The validation rules require callback method to be public, and with CodeIgniter's magical routing, the methods can be accessed using URLs.
To overcome this, I use the following:
PHP Code:
$this->form_validation->set_rules('field''Name''callback__validator');

public function 
_validator($str='') {
//logic

Reply




Theme © iAndrew 2016 - Forum software by © MyBB