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 pages class is extending the CI_Controller class. This means that the new pages class can access the methods and variables defined in the CI_Controller class ( system/core/Controller. php ). The controller is what will become the center of every request to your web application.
$this refers to the current object. Whenever you create an instance like this: $something = new SomeClass(); Then $this refers to the instance that is created from SomeClass , in this case $something .
Class Constructors If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it: parent::__construct(); The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.
A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy. Views are never called directly, they must be loaded by a controller.
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.
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:
Controller
functions are mapped to parts of the URL (controller/function
)there are functions in the controller which should NOT be mapped to the URL
- they are declared as `private` (available since PHP5)
OR
- their names start with `_` (works also for PHP4)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With