I have started playing with CodeIgniter now.
And I use their user guide and other third-party tutorials available for learning. I'm a bit stuck at the naming of private functions. Based on the user guide, I have to prefix a _
in the name of private functions. But in this tutorial, check the Add Logout section. In it, there is a private function: private function check_isvalidated()
. Here, it is not prefixed with the character _
.
So that's also an accepted naming convention ?
At the same time, there is another one called _output()
: Processing Output.
It's a public function with the naming convention of a private function !
It's a bit of confusing when I try to learn in a systematic manner.
The _
prefix is a convention for functions defined in the CONTROLLER
.
The user guide says:
In some cases you may want certain functions hidden from public access. To make a function private, simply add an underscore as the name prefix and it will not be served via a URL request.
http://www.codeigniter.com/user_guide/general/controllers.html#private-methods
Adding an _
is CodeIgniter's own way of declaring functions in the controller (only in the controller) that cannot be called directly by the user:
Regarding _output
function, it is public, but it cannot be called directly since it contains _
.
Why is public?
The function is called by the system, so it needs to be accessible from outside the class, it is not a private function. But, it contains _
to make sure it is not called via the URL.
To sum up, if you have functions in your controller which you don't want to be called directly via the url, add _
prefix OR use the private
access operator. Either one of them is good enough.
FYI, other frameworks like Yii or Zend framework, use the action
prefix for all controller functions which CAN be called via the URL (are mapped).
While the user guide does say that you have to prefix the function name for private functions inside a controller with an underscore, it is not mandatory to do so. Although, it might be a good idea to follow the convention and it is recommended that you do so.
The noticeable effect when prefixing the function name with an underscore can be seen if the access modifier is public
. In this case, if you try to access the function via URL will give you a 404
error. But in the case, that you have the access modifier set to private
it does not matter whether if you prefix the function name with an underscore.
But in this tutorial, check the Add Logout section. In it, there is a
private function: private function check_isvalidated(). Here, it is
not prefixed with the character _.
In that tutorial, the function name is not prefixed with an underscore, but it is a private function because it is declared to be one. Thus, trying to access it via URL will not work.
At the same time, there is another one called _output(): Processing
Output. It's a public function with the naming convention of a private
function!
I've already explained this, but I want to point out that the _output()
function is one of those special functions that will be called at a certain point during a script execution. In this case, CodeIgniter will call this function at the end of the function, right when it's time to output something to the browser.