Home  >  Article  >  Backend Development  >  Naming conventions and specifications for PHP OOP functions

Naming conventions and specifications for PHP OOP functions

WBOY
WBOYOriginal
2024-04-11 10:36:01398browse

PHP OOP function naming convention includes the use of Pascal nomenclature (high camel case for class names and interface names) and underscores (member variables, constants, function and method names). The naming convention specifies the use of access control characters (public, protected, and private) and prefix conventions (double underscore means private, single underscore means protected). Practical examples show how to define classes, member variables, and methods according to these conventions.

PHP OOP 函数的命名约定与规范

Naming conventions and specifications for PHP OOP functions

Naming convention:

  • Use Pascal naming Method (big camel case naming method) represents class names and interface names
  • Use lowercase letters and underscores to represent member variables (instance variables) and class constants
  • Use lowercase letters and underscores to represent function and method names ( Do not start with a double underscore)

Specification:

Classes and Interfaces:

  • Class Definition Should contain the __construct() constructor.
  • Public methods should start with the public access control character.
  • Protected methods should start with the protected access control character.
  • Private methods should start with the private access control character.

Member variables:

  • Private member variables should be prefixed with a double underscore.
  • Protected member variables should be prefixed with an underscore.

Constants:

  • Class constants should be named ALL_CAPS in uppercase letters.

Functions and methods:

  • should be named using the verb-noun or noun-verb format.
  • Negative words should be avoided.
  • Affirmative words should be used to express the results of the operation.

Actual case:

Create the following file User.php:

class User
{
    private $_name;
    private $_email;

    public function __construct($name, $email)
    {
        $this->_name = $name;
        $this->_email = $email;
    }

    public function getName()
    {
        return $this->_name;
    }

    protected function getEmail()
    {
        return $this->_email;
    }

    private function isValidEmail()
    {
        return filter_var($this->_email, FILTER_VALIDATE_EMAIL) !== false;
    }
}

Use the above class:

$user = new User('John Doe', 'john.doe@example.com');
echo $user->getName(); // John Doe

The above is the detailed content of Naming conventions and specifications for PHP OOP functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How PHP functions executeNext article:How PHP functions execute