Best syntax for function names in PHP [closed]

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 for the sake of, say, "Hygienization" of the code, I would like to know which is cleaner, example 1 or example 2?

    
asked by anonymous 01.09.2016 / 05:30

3 answers

6

There is no correct syntax, this goes from the taste of each one, what I will mention are the differences

  • camelCase

    In camelCase there may be variations in the use of upper and lower case, but in practice we usually use something like FooBaz (known as camelCaps ) for class names and namespaces and 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, eg:

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

    In snake case, we usually write everything in tiny and use the underline / underscore to split the words lower_case_with_underscores , this is quite common in procedural PHP codes and native functions, in Python it's also a bit common to use it, it's not so common to use in classes, example:

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

      

    I do not know if it's the same thing, but I think it can be the same as StudlyCaps

    Sometimes called UpperCamelCase or Dromedary Case is very similar to camelCase, but the difference is that we always write the initial letter in uppercase.

    , for both class names and methods (I think it's the most common in C #), example:

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

But what really matters is never mixing the two styles, it will not do any harm, but for sure it would confuse you a lot, choose a style and just use it.

PSR

Today many web-applications like Laravel and cakephp use the composer autoload, which is based on the PSR ( link ), the PSR is a series of recommendations (not required), these applications generally follow some standards described here link in particular the PSR-1 and PSR-2 :

  • Files use only <?php echo and <?= (ie do not use things like <? echo )
  • Files must be UTF-8 without BOM
  • Namespaces and MUST classes should always use autoloading PSR: [PSR-0 , < a PSR-4] .
  • Class names should always be declared with StudlyCaps .
  • Constants in classes should be in BOX_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 composer-autoload, files can conflict with namespaces and classes if the files do not follow the same style. For example if the class is called this:

<?php

use Foo\Bar\Baz;

include 'autoload.php';

new Baz;

The file must be with this path 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.

    
01.09.2016 / 06:06
2

As was said earlier by Guilherme: there is no correct one. There is indeed a recommendation, which is well demonstrated in PSR-2 - a coding style.

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

If we have to address this issue of library development, I strongly recommend that you use the PSR-2 standards.

It is easy to see that most libraries (% with%,% with%,% with%,% with%) use the default Zend (it deals with other subjects, and not only from the nomenclature of methods).

I'm not saying that you should do everything the same as these libraries do, but it is important, in developing libraries, to maintain a standard, to facilitate users accustomed to the standards employed in libraries to use yours.

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

Note, for example, the summary of the Laravel interface:

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 method names are in CamelCase (as explained by Guilherme).

PSR Summary (for your case)

As a Summary of the PSR-2 (which I remember head), I can state that the standards are:

  • Use camelCase to name the methods of a class.

  • The words Guzzle or Gregwar should come before the visibility ( PSR-2 , ArrayAccess or final ) of the methods.

  • Use snake_case for functions.

  • Function keys or methods should contain a line break.

Again stressing that this is a recommendation, it is not required.

    
01.09.2016 / 14:20
1

Personally, I like to use the camelCase style.

The choice is more related to the personal taste or pattern followed by the development team.

I recommend reading the following posts (they are short) link link

    
01.09.2016 / 14:03