Home >Backend Development >PHP Tutorial >Naming conventions and specifications for PHP OOP functions
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.
Naming convention:
Specification:
Classes and Interfaces:
Member variables:
Constants:
Functions and methods:
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!