Home >Backend Development >PHP Tutorial >PHP code writing specifications
Do not add unnecessary context
If your class name or object name has a specific meaning, please do not repeat the name of the variable.
Poor:
<?php class Car{ public $carMake; public $carModel; public $carColor; //... }
Good:
<?php class Car{ public $make; public $model; public $color; //... }
Number of function parameters (ideally less than 2)
Limit function parameters The number of parameters is very important because it makes the function easier to test. With more than three parameters, you have to test a lot of different situations with each individual parameter.
No parameters is the ideal situation. One or two parameters are OK, but three should be avoided. Usually, if you have more than two parameters, then your function is trying to do too much. If not, most of the time, a higher-level object will suffice as a parameter (Translator's Note: such as an array, object).
Poor:
<?php function createMenu($title, $body, $buttonText, $cancellable) { // ...}
Good:
<?php class MenuConfig { public $title; public $body; public $buttonText; public $cancellable = false;}$config = new MenuConfig();$config->title = 'Foo';$config->body = 'Bar';$config->buttonText = 'Baz';$config->cancellable = true;function createMenu(MenuConfig $config) { // ...}
A function should only do one thing
This is the most important rule in software engineering. When functions do more than one thing, they are harder to write and test. When you can isolate a function into an action, it can be easily refactored and the code will be more readable.
Poor:
<?phpfunction emailClients($clients) { foreach ($clients as $client) { $clientRecord = $db->find($client); if ($clientRecord->isActive()) { email($client); } }}
Good:
function emailClients($clients) { $activeClients = activeClients($clients); array_walk($activeClients, 'email'); } function activeClients($clients) { return array_filter($clients, 'isClientActive'); } function isClientActive($client) { $clientRecord = $db->find($client); return $clientRecord->isActive(); }
Use get and set methods
In PHP, you can set the public, protected and private keywords for methods , you can control the visibility of properties on an object. This is part of the open/closed principle of object-oriented design.
Poor:
class BankAccount { public $balance = 1000; } $bankAccount = new BankAccount(); // Buy shoes... $bankAccount->balance -= 100;
Good:
class BankAccount { private $balance; public function __construct($balance = 1000) { $this->balance = $balance; } public function withdrawBalance($amount) { if ($amount > $this->balance) { throw new \Exception('Amount greater than available balance.'); } $this->balance -= $amount; } public function depositBalance($amount) { $this->balance += $amount; } public function getBalance() { return $this->balance; } } $bankAccount = new BankAccount(); // Buy shoes... $bankAccount->withdrawBalance($shoesPrice); // Get balance $balance = $bankAccount->getBalance();
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of PHP code writing specifications. For more information, please follow other related articles on the PHP Chinese website!