Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 - Naming Convention & Style Guide
#1

(This post was last modified: 06-01-2020, 08:35 AM by jreklund.)

Stackoverflow link: https://stackoverflow.com/questions/6212...tyle-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.
Reply
#2

(This post was last modified: 06-01-2020, 08:08 AM by dave friend.)

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

(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
Reply
#4

@myo: You have an outdated userguide, please use the one on the main page: https://codeigniter.com/user_guide/incom...llers.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().
Reply
#5

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/tutor...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
Reply
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB