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 theCONTROLLER.The user guide says:
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:Controllerfunctions are mapped to parts of the URL (controller/function)there are functions in the controller which should NOT be mapped to the URL
Regarding
_outputfunction, 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 theprivateaccess operator. Either one of them is good enough.FYI, other frameworks like Yii or Zend framework, use the
actionprefix for all controller functions which CAN be called via the URL (are mapped).