Coding Standards - Mumtahina-Mim/Job-Portal-Website GitHub Wiki

Introduction:

PHP is a server side scripting language. that is used to develop Static websites or Dynamic websites or Web applications. PHP stands for Hypertext Pre-processor, that earlier stood for Personal Home Pages.PHP scripts can only be interpreted on a server that has PHP installed.The client computers accessing the PHP scripts require a web browser only.A PHP file contains PHP tags and ends with the extension “.php”.We are going to use python 7.0.1 version for our project

Variable Naming Convention:

Variable names should be meaningful. Instead of x, we can write first_number.

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variables can't start with &, % characters
  • Variable names are case-sensitive ($age and $AGE are two different variables)

Example:

         #Legal variable names:
          $myvar = "John"
          $my_var = "John"
          $_my_var = "John"
          $myVar = "John"
          $MYVAR = "John"
          $myvar2 = "John"

          #Illegal variable names:
          $2myvar = "John"
          $my-var = "John"
          my var = "John"

Method Naming Convention:

Method names should have lowercase letters with words separately by underscores.To avoid name clashes with sub-classes.PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.

Example:

          function writeMsg() {
           echo "Hello world!";
            }

           writeMsg(); // call the function

Output: Hello world!

Constant Naming Convention:

Constants are usually defined on a module level and written in all capital letters with underscores separating words.

Example:

        PI = 3.14
        MAX_NUMBER = 1000

Class Naming Convention:

Constants as the name intends are meant to store the values which don’t change throughout the program. So what kind of values are supposed to be constants? They are Database Credentials, API Keys and Mathematical constants as well. There are two types of constants you can find in PHP language – Global and Class-based.

Example:

       define(name, value, case-insensitive)
       define(‘DB_HOST’, ‘localhost’);

Instance Variables

Objects are also known as instance. Member Variable − These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.

Example:

                <?php   
                      class Myclass
                           {
                              public $font_size =10;
                           }
                      $f = new MyClass;
                      echo $f->font_size;
                ?>

Class variables

  • Class variable names should be all lower case
  • Words in an class variable name should be separated by a space and $

Example:

            class Fruit {
                var $name;
                public $color;
                )

Package Naming Convention:

All package names start with an uppercase character and usually are written in UpperCamelCase. In order to avoid problems with different filesystems, only the characters a-z, A-Z, 0-9 and the dash sign “-” are allowed for package names – don’t use special characters.

The full package key is then built by combining the vendor namespace and the package, like Neos.Eel or Acme.Demo.

Modules:

One of the problems often encountered when working with independently developed code are class name collisions. For example, a lot of modules come with an admin controller. To avoid collisions, in Fuel every module lives in its own PHP namespace, which must be named identical to the folder name of the module.

Example:

          namespace Mymodule;

             class Controller_Widget
                     {}

Method Arguments:

PHP supports passing arguments by value (the default), passing by reference, and default argument values. Variable-length argument lists and Named Arguments are also supported. As of PHP 8.0. 0, the list of function arguments may include a trailing comma, which will be ignored.

Example:

         function takes_array($input)
          {
              echo "$input[0] + $input[1] = ", $input[0]+$input[1];
          }

Indentation:

Spacing and indentation should be consistent throughout your code. Many developers choose to use 4-space or 2-space indentation. In PHP, each nested statement (e.g., a statement following a "{" brace) should be indented exactly once more than the previous line's indentation. In this project, we will use 4 spaces per indentation level.

Example:

         if number % 2 == 0: 
             print(“Even number”) 

Blank Lines:

Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

Example:

  <?php
     echo "If you view the page source \r\n you will find a newline in this string.";
     echo "<br>";
     echo nl2br("You will find the \n newlines in this string \r\n on the browser window.");
  ?>        

Imports:

The ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces. This is similar to the ability of unix-based filesystems to create symbolic links to a file or to a directory.

PHP can alias(/import) constants, functions, classes, interfaces, and namespaces.

Example:

            namespace foo;
            use My\Full\Classname as Another;
            use ArrayObject;
            use function My\Full\functionName as func;

Comments:

Use the Syntax "// text" and "/* text /" Comments are usually written within the block of PHP code to explain the functionality of the code. PHP multi-line line comment begins with /, and ends with */.

Example:

        echo "Hello World!"; // Output "Hello World!"
          /* The following line of code
            will output the "Hello World!" message */
        echo "Hello World!";

Docstrings:

PHPDocumentor is the most advanced automatic documentation system written for PHP, in PHP. This package has many features: *ability to parse any PHP file, regardless of documentation format *is designed specifically for PHP, and so documents all constants, functions, classes, methods, and class variables *A DocBlock contains three basic segments in this order: Short Description Long Description Tags

Example:

       /**
         * return the date of Easter
         * 
         * Using the formula from "Formulas that are way too complicated for anyone to 
         * ever understand except for me" by Irwin Nerdy, this function calculates the 
         * date of Easter given a date in the Ancient Mayan Calendar, if you can also
         * guess the birthday of the author.
       */