• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Naming / referring to controllers and models

#11
(11-10-2016, 12:17 PM)codeguy Wrote:
(11-07-2016, 03: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!

Use the user guide for the version of CodeIgniter you have installed, and there should be no confusion.

There could still be confusion searching online, as outdated webpages may still refer to version 2, which is now alegacy version and no longer supported.

Yeah, For the last 4 months I've been reading every online article and YouTube tutorial I could find on CI. I've also paid for a few online courses thru Envato, Udemy, Lynda etc. I don't remember seeing anything about CI versions having different syntax requirements. I could have missed it though. I suppose the authors would want to create the impression that their video or web site advice could be universally applied to any CI project using any version - which is the impression I came away with.   Confused

The difference is only in the file naming conventions.
Reply

#12
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.
Reply

#13
(11-11-2016, 10: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, 10: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, 10: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, 10: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, 10: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.
Reply

#14
@codeguy,

For future reference   Big Grin
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.