Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter PHP Style Guide
#1

[eluser]TheFuzzy0ne[/eluser]
Hi everyone. I have a quick question:

When you create a class that extends an existing class (using your prefix), should the class name be like this:

MY_Controller

or like this:

My_controller

I don't think it makes a difference, but I was looking in the [url="http://ellislab.com/codeigniter/user-guide/general/styleguide.html"]PHP Style Guide[/url], and it says:

Quote:Class names should always have their first letter uppercase, and the constructor method should match identically.

Perhaps this is open to interpretation? Has anyone ever had case-sensitivity issues with PHP class names?

Thanks in advance.
#2

[eluser]Dam1an[/eluser]
I've not had any issues, but I always do MY_Controller, MY_Model etc
And I can't remember ever seeing it in lower case (MY_controller)
#3

[eluser]TheFuzzy0ne[/eluser]
The reason I ask is that I would like to write a CodeIgniter file validator, which can validate your file (depending on the type), and flag potential errors. I see countless requests on the forum asking for help because CodeIgniter is not working as expected, usually due to people not reading the user guide fully, or not understanding it. It's sometimes quite hard to validate a problematic file without actually having the file, so I thought I should save us all some time by creating a file validator. I'm hoping that as well as helping to make files work, it might encourage people to adhere to the practices suggested in the PHP style guide.

I'm not sure whether I should check the class case and flag it as an error, a warning, or just not bother checking for it.

And before you ask, Dam1an - Yes, I have plenty on my plate, but I want another project that I can work on in my free time, that will hopefully benefit the community.
#4

[eluser]Dam1an[/eluser]
How did you know I was going to ask that question? lol

If you want, I'll fire up my Linux VM see if case makes any difference, as Linux is case sensitive
If you want to be really clever with the validator, it could detrmine if its a Linux/Windows/Mac OS and determine if its a warning/error based on that (case would be an error on Linux but only a warning on Windows)
#5

[eluser]TheFuzzy0ne[/eluser]
Not a problem. I have a Debian server sitting right next to me.

I have tried the following code with Windows and Linux, and it doesn't seem to make any difference. But I'm not sure if I missing something:

Code:
class MY_Class
{
    function MY_Class()
    {
        echo "class instantiated!\n";
    }

    function MY_FUNCTION()
    {
        echo "MY_FUNCTION() called\n";
    }
}

$class = new my_class();
$class->my_function();

# Output (both Windows and Linux):
#
# class instantiated!
# MY_FUNCTION() called

I can only conclude that this is more about consistency than anything else. Every framework has naming conventions, even if they're just their for consistency. My validator will be based upon the style guide, so I think that this should be flagged as a warning, unless of course it really can cause problems.
#6

[eluser]Dam1an[/eluser]
Have you tried with the file name in differant case? eg my_controller.php, MY_controller.php etc
Other cases to look out for are if a non library has the filename capitalized... (I assumed you where refering to filenames all along, not class declerations lol)
#7

[eluser]TheFuzzy0ne[/eluser]
File names are a different story altogether, and as such, will be flagged as being an error if they aren't in the correct case. Experience on these forums tells me that file case is not an issue for Windows systems, but obviously it is for Linux systems.

I'm guessing here, but I would have though that there is no correlation between the case of class names and the case of file names on a Windows system. You can have a file called MY_CONTROLLER.php and the class defined as My_CoNtRoLlEr if you want. It should make absolutely no difference whatsoever.
#8

[eluser]Natebot[/eluser]
A file validator would be a welcome tool Fuzzy.

Could the validator load the CI config file to get the setting for subclass_prefix and use it to validate the class names and file names found in the libraries directory? In that way the validator is agnostic as to the case of the prefix but it just makes sure the class uses the same case. Maybe this validation behavior can be overridden by a config setting that tells the validator require the subclass_prefix to be capitialized, etc.

Maybe it can even issue a warning if there appears to be a prefix mismatch such as when a class is "FB_Controller extends Controller" and $config['subclass_prefix'] = "My_";
#9

[eluser]Colin Williams[/eluser]
The class name shouldn't be the issue (PHP is case insensitive). The file name is what's important, and it should be MY_Controller.php. The "MY_" is the prefix. Everything thereafter should follow the naming conventions.
#10

[eluser]TheFuzzy0ne[/eluser]
[quote author="Natebot" date="1243128293"]Could the validator load the CI config file to get the setting for subclass_prefix and use it to validate the class names and file names found in the libraries directory? In that way the validator is agnostic as to the case of the prefix but it just makes sure the class uses the same case. Maybe this validation behavior can be overridden by a config setting that tells the validator require the subclass_prefix to be capitialized, etc.[/quote]

Initially, I don't consider that a priority, but I have thought about it to some extent. I'm planning on having the validator running as a Web site primarily, just to help people new to CodeIgniter, or people who are interested in changing their code to fulfil the suggestions laid out in the PHP style guide. There will probably be a lot more tests that will be added over time, but when it's fully tested, I plan to create a PHP command line version, which will be able to run locally and traverse the directory structure, giving you a detailed report.

I've also been thinking about releasing a version based on [url="http://wapache.sourceforge.net/"]Wapache[/url], which will essentially allow you to run the Web site locally. I will add support for uploading and extracting a zip file, where the config file can be used to set the prefix, or it can be specified via a Web-based GUI inside Wapache. I have created a really simple Wapache app to test it, but I've yet to fully explore it and discover it's full potential.

Hopefully this all makes sense. It's been a very long day...

[quote author="Natebot" date="1243128293"]Maybe it can even issue a warning if there appears to be a prefix mismatch such as when a class is "FB_Controller extends Controller" and $config['subclass_prefix'] = "My_";[/quote]

At the moment, I plan for the system to make an educated guess at whether or not the class is prefixed. This only applies to libraries in this case, (as helpers don't have a class name, of course), so by checking to see if the class extends known existing core library should be enough to work out if there's a prefix. I assumed it was the de facto to make you're prefixes in uppercase, but maybe that's not such a smart assumption. In any case, as PHP is case insensitive, it certainly won't cause any problems.

[quote author="Colin Williams" date="1243129015"]The class name shouldn't be the issue (PHP is case insensitive). The file name is what's important, and it should be MY_Controller.php. The "MY_" is the prefix. Everything thereafter should follow the naming conventions.[/quote]

So, should class case be enforced by the validator, or simply ignored? The main idea is to help fix really obvious errors that can cause the loader problems - incorrect file case, class names not matching the file name, wrongly named constructors, and so on - but I thought I may as well incorporate the PHP style guide into that to, to hopefully promote consistency.




Theme © iAndrew 2016 - Forum software by © MyBB