Naming / referring to controllers and models |
(11-10-2016, 11:17 AM)codeguy Wrote:(11-07-2016, 02:33 PM)ciadmin Wrote: If you mix readings from the version 2 user guide (where class filenames were lower-case) with readings from the current version 3 userguide (where class filesnames are UCfirst), you will find conflicting information! The difference is only in the file naming conventions.
After re-reading this thread several times can I now say (for CI 3.x.x) ?
1) Class names in class definitions must be UC fist. 2) Filenames (in the models and controllers folders) that hold the class definitions must be lower case first. 3) When referencing a model or controller class in a CI statement reference the filename that holds the class, not the class name. Here's where I feel I'm on shaky ground: a) Must all other characters in class names be LC? For example 'CI_MyController' has UC characters after first. Maybe that's just for CI reserved class names? Although the example is not a CI reserved class name I have seen tutorials that suggest the creation of such a class to extend and replace the CI_Controller class in projects. b) Are there exceptions to rule 3) above ? Sorry to be so picky. I'm looking for a sign I can put on the wall just above my monitors to prevent from writing code full of mysterious 'internal server errors'. Thanks in advance. (11-11-2016, 09:53 AM)codeguy Wrote: 1) Class names in class definitions must be UC fist. Yes, that's the rule, although class names in PHP are not case-sensitive. (11-11-2016, 09:53 AM)codeguy Wrote: 2) Filenames (in the models and controllers folders) that hold the class definitions must be lower case first. No, file names must MATCH the class names they declare, and therefore if a file declares a class, it must be named in Ucfirst too. (11-11-2016, 09:53 AM)codeguy Wrote: 3) When referencing a model or controller class in a CI statement reference the filename that holds the class, not the class name. You've put it very ambigiously, as "referencing" can mean a lot of things, but no ... This only matters when loading models: $this->load->model('Foo_model') will assign the model object to $this->Foo_model $this->load->model('bar_model') will assign it to $this->bar_model Same thing doesn't apply to libraries - they'd always be assigned to e.g. $this->library_name (lower-case). The most safe thing to do is to use lower-case in your code. (11-11-2016, 09:53 AM)codeguy Wrote: a) Must all other characters in class names be LC? For example 'CI_MyController' has UC characters after first. Maybe that's just for CI reserved class names? Although the example is not a CI reserved class name I have seen tutorials that suggest the creation of such a class to extend and replace the CI_Controller class in projects. No. All file names (declaring classes) must be in Ucfirst, only the first character being in upper case. (11-11-2016, 09:53 AM)codeguy Wrote: Sorry to be so picky. I'm looking for a sign I can put on the wall just above my monitors to prevent from writing code full of mysterious 'internal server errors'. Thanks in advance. You're over-thinking it really ... If you declare a class, make it's first letter a capital, use lower-case for literally everything else.
|
Welcome Guest, Not a member yet? Register Sign In |