Search code examples
phpcoding-stylenaming-conventionscamelcasingcodeigniter-4

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.


Solution

  • The first link is about Controllers, they have a functional requirement for UC-first style. This is needed to accommodate CI's direct mapping of URL segments to controller/method/parameter.

    Not documented is that if a route is defined for any given controller then the UC first rule is not required. In that case, UpperCamelCase style SHOULD be used as it says in the second of the referenced links.

    Keep in mind that UpperCamelCase refers to class naming. So that style does not apply to helpers. Those should use a "procedural" coding style.

    The second link is otherwise the coding standard to follow.

    Please keep in mind that the coding standard as found at the second link is about contributing code to the framework. For your own projects feel free to write code with any "style" you like. The only exception to that thought is the needs of Controller naming.