PHP code writing specifications

Guanhui
Release: 2023-04-08 15:36:01
forward
2441 people have browsed it

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;
    //...
    }
Copy after login

Good:

<?php class Car{
    public $make;
    public $model;
    public $color;
    //...
    }
Copy after login

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) {
    // ...}
Copy after login

Good:

<?php class MenuConfig {
    public $title;
    public $body;
    public $buttonText;
    public $cancellable = false;}$config = new MenuConfig();$config->title = &#39;Foo&#39;;$config->body = &#39;Bar&#39;;$config->buttonText = &#39;Baz&#39;;$config->cancellable = true;function createMenu(MenuConfig $config) {
    // ...}
Copy after login

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);
        }
    }}
Copy after login

Good:

function emailClients($clients) {
    $activeClients = activeClients($clients);
    array_walk($activeClients, &#39;email&#39;);
}
function activeClients($clients) {
    return array_filter($clients, &#39;isClientActive&#39;);
}
function isClientActive($client) {
    $clientRecord = $db->find($client);
    return $clientRecord->isActive();
}
Copy after login

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;
Copy after login

Good:

class BankAccount
{
    private $balance;
    public function __construct($balance = 1000)
    {
      $this->balance = $balance;
    }
    public function withdrawBalance($amount)
    {
        if ($amount > $this->balance) {
            throw new \Exception(&#39;Amount greater than available balance.&#39;);
        }
        $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();
Copy after login

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!

Related labels:
php
source:zhihu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!