CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

More consistency in capitalization

Open vlakoff opened this issue 14 years ago • 31 comments

Hi, I'm always confused with capitalization in CI, so I decided to gather the current behavior:

Controllers

  • filename has to be lowercase

Models

  • filename has to be lowercase
  • $CI->load->model('themodel') can be lower or uppercase, but:
    • $CI->themodel has to use the same case
    • if $CI->load->model('themodel') then $CI->load->model('Themodel'), the class is loaded in CI twice

Libraries

  • filename can be lower or uppercase (edit: has to be capitalized as of 3608e1a094945631c5b65e1f66460e4486c5b541)
  • $CI->load->library('thelibrary') can be lower or uppercase
  • $CI->thelibrary can only be lowercase

Helpers

  • filename has to be lowercase
  • $CI->load->helper('thehelper') can be lower or uppercase

$routing['controller'] override in index.php

  • has to be the same case as the controller filename, so lowercase, and without the .php extension

As you can see, there are some discrepancies.

What are you suggestions to improve consistency and/or flexibility, without breaking existing apps?

vlakoff avatar Sep 16 '12 14:09 vlakoff

This can prove to a problem for those that develop on a Windows environment, and then deploy in a Linux environment. From your notes, lowercase appears in each of the scenarios, so I would say for personal preference stick with lowercase whenever possible. Less issues between environments. The exception to the rule is when you are overriding a system library/helper. You file and class need to match the original case.

pickupman avatar Dec 01 '12 16:12 pickupman

As of 3608e1a094945631c5b65e1f66460e4486c5b541, library file names can no longer be in all-lowercase.

On another note - property names are case-insensitive and this doesn't depend on no condition, they just are. Therefore it doesn't matter if it's $this->library, $this->Library, $this->whatEver, etc.

narfbg avatar Jan 29 '13 13:01 narfbg

To clarify, most of this case-insensitivity is because of PHP, not CodeIgniter.

timw4mail avatar Jan 29 '13 13:01 timw4mail

So we have an all-lowercase filename rule, except for libraries which have to be capitalized. Not too hard yet, but not optimal. However I guess that can't be improved.

The points that might be improved IMHO are:

  • What I described about load->model(). Having load->model('foo') and load->model('Foo') at different locations of an app doesn't sound inconceivable to me.
  • $routing['controller'] override in index.php might be more resilient (see 4876b4837546c8c9f659bb74c68b16cdb326c844 for an example of an issue I ran into)

vlakoff avatar Jan 29 '13 14:01 vlakoff

The general rule that we're going towards is classes in Ucfirst and anything else in lower-case. This could cause backwards compatibility issues for controllers and models however and is still discussed with EllisLab.

Note that I'm talking about filenames only here as this is the only thing that cannot be solved by PHP itself if you didn't follow the rule. With that said, the points you've listed should be considered bugs if it is indeed a problem - when you have a rule of how should the files be named, then we can easily strtolower() and/or ucfirst() the input string.

narfbg avatar Jan 29 '13 16:01 narfbg

Wasn't the point of checking for multiple variations of the filename to support the differences between Windows and Linux? I'm all for providing more consistent conventions recommended in the user guide (I've been matching my file names to the class name for a long time), but I thought that's why we also checked the all-lowercase filenames, too?

On another note - property names are case-insensitive and this doesn't depend on no condition, they just are. Therefore it doesn't matter if it's $this->library, $this->Library, $this->whatEver, etc.

They are case sensitive, just like variables. Functions and methods, however, are case insensitive (maybe that's what you're thinking of).

AkenRoberts avatar Jan 29 '13 23:01 AkenRoberts

Um, no ... I was thinking of class names and given that they are case-insensitive I'd assume properties are as well. I've never actually tried mismatching the case however, lol.

narfbg avatar Jan 30 '13 00:01 narfbg

Haha gotcha.

AkenRoberts avatar Jan 30 '13 00:01 AkenRoberts

On another thought, CI_Controller::__get() already exists, so fixing the property issue shouldn't be a problem.

narfbg avatar Jan 30 '13 00:01 narfbg

what needs to be done to get this issue rolling?

ragboyjr avatar Apr 06 '13 04:04 ragboyjr

Well, what do you suggest?

AkenRoberts avatar Apr 06 '13 05:04 AkenRoberts

Well, it seems like everything is going to psr-0 naming conventions, I think it would be easiest if we followed something similar with models, and libraries. It seems to make things much simpler when the class name you load is the same name as the file. And you could always add backwards compatible (deprecated) feature that will strtolower the file name if the name given doesn't exist. i.e. user calls $this->load->library('Lib_NAME'), so we'd check ./application/libraries/Lib_NAME.php, if doesn't exist then we load ./application/libraries/lib_name.php. That's kind of how a psr0 loader works which makes sense to me, and seeming how every other php framework out there follows this model, it must make sense to others too. I think it'll also allow more flexibility for codeigniter in the future if wanted to support namespaces, autoloading, composer, etc... (not to say I want to propose any of those changes).

As far as helpers go, they really are just a prettier .inc file, and it's the convention to make inc files lowercase, so I think we should keep helpers the same (all lower case) as far as capitalization goes.

Examples

Here are just some examples of what users would code, and what the actions created would be.

The following code is not independent, and I figure $this->load->model and $this->load->library should work exactly the same as far as including files, capitalization, and variable names are concerned.

// essentially calls require './application/models/Model.php'
$this->load->model('Model');
$this->model->func();

/*
 * tries to call require './application/models/MODel.php'
 * if doesn't exist then calls './application/models/model.php
 * it will overwrite the $this->model, unless a second parameter of var name
 * was specified. 
 */
$this->load->model('MODel');
$this->model->func();

// tries to load require './application/models/moDEL.php'
$this->load->model('moDEL', 'moDEL');
$this->moDEL->func();

/*
 * tries to call require './application/models/Model/Stuff.php'
 * if that fails, then it tries './application/models/model/stuff.php'
*/
$this->load->model('Model/Stuff.php');
$this->stuff->func();

/*
 * tries to call require './application/models/Model/Foo.php'
 * if that fails it tries './application/models/model/foo.php'
 * The instance name will be strtolower(str_replace('\\', '_')),
 * and the class name to instantiate will be
 * $model = new Model\Foo();
 */
$this->load->model('Model\Foo');
$this->model_foo->func();

ragboyjr avatar Apr 06 '13 06:04 ragboyjr

Sounds very good.

I don't understand why library filenames, and only them, would have to be capitalized, this just doesn't make sense.

Meanwhile, the Model loading discrepancies I mentioned in my OP would happily be fixed ;-)

vlakoff avatar Apr 06 '13 12:04 vlakoff

@ragboyjr Don't bother with PSR-0 for the time being. We are indeed most likely going that way, but due to support for PHP 5.2.4, it's technically impossible at this point.

@vlakoff I believe I already mentioned that there are plans for everything to go Ucfirst.

Glad you brought this issue again though, it's a release-blocker. It was easy to switch libraries, as custom libraries are not vital for a CI application and lower-case names for them are not that common. Controllers and models are however and we need to work out a smoother transition for them. I'm thinking of still allowing them to be all-lowercase, but deprecate them and log an error message when a non-Ucfirst file is loaded. Anybody got a better idea?

narfbg avatar Apr 08 '13 06:04 narfbg

@narfbg Can you please edit your first post on this topic? You wrote:

On another note - property names are case-insensitive and this doesn't depend on no condition, they just are. Therefore it doesn't matter if it's $this->library, $this->Library, $this->whatEver, etc.

That's wrong.

I've just double-checked, it doesn't work. There is no stuff like __get(). A capitalization error just throws a PHP notice (and of course the property isn't obtained), maybe that's why you missed it.

vlakoff avatar Apr 08 '13 12:04 vlakoff

You should've just read the rest of the comments. :) This isn't a wiki page ... if I edit something, I should also delete another few posts.

narfbg avatar Apr 08 '13 13:04 narfbg

I'm down with @narfbg's solution, we'd just need to stress this fact in the documentation. Btw, for ci 3.0 does the loader support namespaced loading?

ragboyjr avatar May 06 '13 02:05 ragboyjr

What do you mean? Namespaces are not natively handled at all since the minimum PHP requirement is still 5.2.x.

AkenRoberts avatar May 06 '13 04:05 AkenRoberts

Doesn't codeigniter support 5.2+? So it wouldn't be able to support php code that is 5.3+ which would be a deterrent for people using CI 3.0 with php 5.3+. A lot of new code is written using namespaces, so people (myself included) wouldn't be able to port namespaced code into Codeigniter without the hassle of renaming all of the files

ragboyjr avatar May 06 '13 04:05 ragboyjr

It's not that it can't handle namespacing, it's that it isn't meant to. You can use namespaces and whatever 5.3 or 5.4+ code you want in CodeIgniter, you just need to set it up properly. Typically that means adding an autoloader that matches whatever file hierarchy you have. If you use Composer, all you need to do is add one require statement to your index.php (or similar), and you can use those libraries like you would anywhere else.

You shouldn't want or need to "port" anything backwards into CI (CI is behind the times, you don't want to dumb your code down if you can help it). You should just use third party code like you would anywhere else, with the new \Namespace\Class syntax or similar.

AkenRoberts avatar May 06 '13 04:05 AkenRoberts