CodeIgniter Forums
Codeigniter 4 - Naming Convention & Style Guide - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Codeigniter 4 - Naming Convention & Style Guide (/thread-76604.html)



Codeigniter 4 - Naming Convention & Style Guide - myo - 06-01-2020

Stackoverflow link: https://stackoverflow.com/questions/62128725/codeigniter-4-naming-convention-style-guide

I have found contradictory rules about Codeigniter 4 naming conventions & style guide. For example following are two guides about class names:
Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.
Class names and namespaces SHOULD be declared in UpperCamelCase, also called StudlyCaps, unless another form is functionally required.

Can somebody clarify about method & class naming convention and whether we have different requirements for controllers, models,helpers, etc?

Can I use following convention:
All Classes: Any_class
Methods: any_method

As URL should always be in lowercase so I think camelCase method names should not be used. What is your opinion?
I know its a matter of personal choice but I want to follow best practices.


RE: Codeigniter 4 - Naming Convention & Style Guide - dave friend - 06-01-2020

I answered this on SO.

I think you're right about method names when it comes to Controllers. For any other class use lowerCamelCase as specified in the coding standard.

Remember that the coding standard is all about contributing code to the framework. Feel free to use any "style" you want in your projects.


RE: Codeigniter 4 - Naming Convention & Style Guide - myo - 06-01-2020

(06-01-2020, 08:02 AM)dave friend Wrote: I answered this on SO.

I think you're right about method names when it comes to Controllers. For any other class use lowerCamelCase as specified in the coding standard.

Remember that the coding standard is all about contributing code to the framework. Feel free to use any "style" you want in your projects.

Thanks


RE: Codeigniter 4 - Naming Convention & Style Guide - jreklund - 06-01-2020

@myo: You have an outdated userguide, please use the one on the main page: https://codeigniter.com/user_guide/incoming/controllers.html

Personally I follow PSR-4. Therefor all classes should be named ClassName. And methods should be lowerCamelCase().

But if you wan't the automatic URI helper to work you need to name them Classname and lowercamelcase().


RE: Codeigniter 4 - Naming Convention & Style Guide - svennd - 08-25-2020

I have to say, I am playing with freshly CI4, and the capitals and use of camelcase are throwing me off big time to even continue learning this; Even the tutorial is not consistent, for example https://codeigniter.com/user_guide/tutorial/static_pages.html --> CI4 uses "Views" but "pages" is all lowercase again;

PHP Code:
if ( ! is_file(APPPATH.'/Views/pages/'.$page.'.php')) 


What is the advantage of having a capital here, except to confuse beginning programmers on a case sensitive system ?

Well I guess its the pain you have to take to pick up a new system; but let me complain a bit on this topic  Tongue


RE: Codeigniter 4 - Naming Convention & Style Guide - Chroma - 08-27-2020

I don't think there is any significance at all, it just looks pretty in the source tree.

I would suggest that you have a style and you stick with it for your own projects.

I use the View as per CI4 and then all my subfolders are all lower case and I put a - dash in where I want to separate words.

The only place it really matters is in the controller names and the real problem comes when you want to use a - or _ in the url. In this case it them must be:

Cool_controller.php with a class name of Cool_controller()

But be very careful if you are developing under Windows, because Windows will not care about case, but then when you deploy under Linux it will care and any sloppiness with regard to case will cause all kinds of confusing errors. Like files not found, controllers not found etc.

Even when developing on a Mac, this can still cause problems.