Best syntax for PHP function names

Asked

Viewed 899 times

0

Greetings, I would like to know the correct syntax for writing functions in php.

for example:

public function load_default_controller() {

}

or

public function loadDefaultController() {

}

The question may be kind of meaningless, but because of, say, "Sanitizing" the code, I’d like to know which one is cleaner, example 1 or example 2?

  • 1

    I updated the response and add on the PSR recommendations.

  • 1

    The cleanest thing is to be consistent, one way or another.

3 answers

8


There is no correct syntax, this goes to the taste of each one, what I will quote you are the differences

  • camelCase

    In camelCase there may be variations of the use of uppercase and minuscule letters, but in practice we usually use something like FooBaz (known as camelCaps) for class names and namespaces and we use fooBar for methods (functions in classes known as StudyCaps), If you look at the native PHP classes and popular frameworks you will notice that most use this style, example:

    <?php
    class FooBar
    {
        public function helloWorld()
        {
             echo 'Hello!';
        }
    }
    
  • snake_case

    In Snake case we usually write everything in minuscule and use the underline/underscore to divide the words lower_case_with_underscores, this is quite common in procedural PHP codes and native functions, in Python it is also a little common its use, it is not so common to use in classes, example:

    <?php
    class foo_bar
    {
        public function hello_world()
        {
             echo 'Hello!';
        }
    }
    
  • Pascal Case

    I don’t know if it’s the same thing, but I think it might be the same as StudlyCaps

    Sometimes called Uppercamelcase or Dromedary Case is very similar to camelCase, but the difference is that we write the initial letter always in upper case, both for class names and for methods (I believe it is the most common in C#), example:

    <?php
    class FooBar
    {
        public function HelloWord()
        {
             echo 'Hello!';
        }
    }
    

But what really matters is never mix the two styles, it will do no harm, but it would surely confuse you too, choose a style and use only it.

PSR

Today many web-applications like Laravel and cakephp use the autoload of Composer, which is based on PSR (http://www.php-fig.org), PSR is a series of recommendations (is not mandatory), these applications usually follow some standards described here http://www.php-fig.org/psr/ in particular the PSR-1 and PSR-2:

  • Files to use only <?php echo and <?= (or don’t use things like <? echo)
  • Files should be UTF-8 NO GOOD
  • Namespaces and MUST classes should always use "autoloading" PSR: [PSR-0, PSR-4].
  • Class names should always be declared with StudlyCaps.
  • Constants in classes must be in CAIXA_ALTA using underscore as separator, for example const FOO_BAR = 1;
  • Class names should always be declared with camelCaps.

spl_autload and Unix-like based systems

Although functions, classes and namespaces are case-insensitive when using spl_autload or Autoload files may conflict with namespace and class names if the files do not follow the same style. For example if the class is called like this:

<?php

use Foo\Bar\Baz;

include 'autoload.php';

new Baz;

The file must be this way src/Foo/Bar/Baz.php, if you do so src/Foo/Bar/baz.php will not find the file on Linux, Mac and BSD systems (servers).

  • Well, PSR for me is the same thing as "Greek", rsrs but I understood in parts, I will study about PSR, otherwise it was well explained, Thanks William Birth.

2

As previously stated by William: there is no correct one. There is a recommendation, which is well demonstrated in PSR-2 - an encoding style.

I agree with the part that William explains about "if you start with a pattern, continue with it to the end". But it is important to note here that there are patterns that are employed by most PHP libraries.

If we have to address this library development issue, I strongly recommend using the PSR-2 standards.

It is easy to notice that nowadays, the vast majority of libraries (Zend, Laravel, Guzzle, Gregwar), use the standard employed by PSR-2 (it deals with other matters, and not only the nomenclature of methods).

I’m not saying that you should do everything these libraries do, but it’s important, in the development of libraries, to maintain a standard, to facilitate users accustomed to the standards employed in libraries to use their.

If you think about how standard PHP classes or interfaces are written, for example, you can use common sense and do something similar, so you can present code closer to the "reality" of the language.

Note for example the interface summary ArrayAccess:

interface ArrayAccess {
    /* Métodos */
    abstract public boolean offsetExists ( mixed $offset )
    abstract public mixed offsetGet ( mixed $offset )
    abstract public void offsetSet ( mixed $offset , mixed $value )
    abstract public void offsetUnset ( mixed $offset )
}

Note that the names of the methods are in Camelcase (as explained by Guilherme).

Summary of PSR (for your case)

As a summary of the PSR-2 (which I remember in my head), I can state that the patterns are:

  • Use camelCase to name the methods of a class.

  • The words final or abstract must come before visibility (public, protected or private) of the methods.

  • Use snake_case for functions.

  • Function keys or methods must contain a line break.

Reinforcing again that this is a recommendation, it is not mandatory.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.