PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 21, 2022

PHP coding standards

 May 21, 2022     php, programming     No comments   

 


File and Directory Naming

There are several conventions to keep in mind when naming your files and directories.

Your file names should be all lowercase and use underscores to separate words. The file extension will typically dictate the language of the file, but it is also helpful to prefix PHP classes with `class-` and functions with `function-`. For example, you might name a PHP class that defines a custom Gutenberg block `class-my-custom-block.php`, or a PHP template part that displays content related to post data as `content-single.php`.

For consistency, use underscores in your directory names as well, keeping in mind that directory names should be pluralized for clarity (e.g., templates). If you have multiple directories within another directory that need separation, use dashes instead of underscores (e.g., js--admin)

Variable Naming

There are a few rules to follow when naming variables.

  • Global variables should be prefixed with g_. Static variables should be prefixed with s_. If a variable is being used across different files, it must be declared as global within the files where it is being used.

  • Variables should always be in lowercase. Multiple words should be separated by an underscore. For example, you’d name a variable “$my_name” instead of “$myName” or “$MyName”.

  • Avoid abbreviations and acronyms unless they are very well known.

Function Naming

Most PHP developers use all lowercase letters for function names, generally with underscores to separate words. Names should be short (less than 32 characters, ideally) but meaningful. PHP does not have namespaces or scope modifiers for functions, so you will want to ensure that your functions don't conflict with other code by using something like the following prefixing system:

```cfg_``` - Configuration functions

```db_``` - Database access functions

```template_``` - Template-related functions

The stated goal of the WordPress codebase is that each function should only do one thing. This allows it to be easily reused in multiple places; it makes abstracting the logic into a class method easier; and it makes understanding what the function does more straightforward. When possible, avoid using verbs in your function names as you will likely find yourself adding more functionality to them later on down the road.

Method Names, Visibility, and Arguments

In order to write good code, it's important to have a good understanding of how methods are used by your language. PHP is a procedural programming language with functions that are triggered by method names and arguments. The idea behind this is that there should be only one instance of a method in any given scope, so if you want to store data in memory or perform an operation on it, the method should be declared public and the data stored or processed within it.

In PHP, variables can be defined publicly or protected based on their type. These types are integers, strings and arrays (the latter being an extension to PHP by Rasmus Lerdorf) as well as most classes that inherit from one of these four types (implemented in PHP as abstract classes). Public data can be accessed from anywhere within the context of its declaration; however, private data cannot be accessed from outside its scope unless explicitly granted access.

Control Structures

Some examples of using spaces correctly:

Wrong:

if($a==$b) echo 'something';

if($a == $b)echo 'something';

Right:

if ($a == $b) { echo 'something'; }

Commenting

In your code, comments are a great way to explain why you've chosen to do something. However, avoid commenting something that is obvious from the code. That just obscures your code without adding anything useful. Comments should help the reader understand why you did things the way you did, not what the code itself does.

Good:

// Check if we're ready to send notifications

if ($this->isReady) {

    $this->sendNotifications();

}

When you are learning to code, take time to learn the conventions used by your language.

It is important to become familiar with the conventions and standards used in your language. While these are often not required, they provide a base from which you can build up your style. Your team may adopt a standard, or your software may require it. If you write code for yourself, then you will at least have something to go off of as you refine your own style.

Conventions will vary depending on the language and even the application where it is used (web vs desktop). You can find these standards documented online for many languages -- some of them even dictate how the language should be written!

  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing