php resource centre

  • about
  • articles
  • tutorials
  • resources
  • certification
Home

Primary links

  • About
  • Articles
  • Tutorials
  • Resources
  • Certification

PHP Coding Standards - III

admin — Tue, 22/08/2006 - 3:47pm

Function Calls

Functions shall be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here's an example:



$var = foo($bar, $baz, $quux);

As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability:

$short = foo($bar); $long_variable = foo($baz);

Function Definitions

Function declarations follow the unix convention:

function fooFunction($arg1, $arg2 = '') {
   if (condition) {
     statement;
   }
   return $val;
}

Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate. Here is a slightly longer example:

function connect(&$dsn, $persistent = false) {
  if (is_array($dsn)) {
     $dsninfo = &$dsn;
  } else {
        $dsninfo = DB::parseDSN($dsn);
     }
  if (!$dsninfo || !$dsninfo['phptype']) {
    return $this->raiseError();
  }
  return true;
}
{mosgoogle}

Objects

Objects should generally be " normalized " similar to a database so they contain only the attributes that make sense. Each object should have Error.class as the abstract parent object unless the object or its subclasses will never produce errors. Each object should also have a create()method which does the work of inserting a new row into the database table that this object represents. An update()method is also required for any objects that can be changed. Individual set()methods are generally not a good idea as doing separate updates to each field in the database is a performance bottleneck. fetchData()and getId()are also standard in most objects. See the tracker codebase for specific examples. Common sense about performance should be used when designing objects.

Naming

Constants should always be uppercase, with underscores to separate words. Prefix constant names with the name of the class/package they are used in. For example, the constants used by the DB:: package all begin with " DB_ " .True and false are built in to the php language and behave like constants, but should be written in lowercase to distinguish them from user-defined constants.Function names should suggest an action or verb: updateAddress, makeStateSelector Variable names should suggest a property or noun: UserName, With Use pronounceable names. Common abbreviations are acceptable as long as they are used the same way throughout the project.Be consistent, use parallelism. If you are abbreviating number as 'num', always use that abbreviation. Don't switch to using noor nmbr.Use descriptive names for variables used globally, use short names for variables used locally.

  • $AddressInfo = array(...);
    for($i=0; $i < count($list); $i++)

Control Structures

These include if, for, while, switch, etc. Here is an example if statement, since it is the most complicated form:

if ((condition1) || (condition2)) {
   action1;
} elseif ((condition3) && (condition4)) {
    action2;
  } else {
    defaultaction;
  }

Control statements shall have one space between the control keyword and opening parenthesis, to distinguish them from function calls. You should use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.

For switch statements:
switch (condition) {
  case 1: {
    action1;
    break;
  }
  case 2: {
    action2;
    break;
  } 
  default: {
    action3;
    break;
  }
}

Including PHP Files

Anywhere you are unconditionally including a class file, use require_once. Anywhere you are conditionally including a class file (for example, factory methods), use include_once. Either of these will ensure that class files are included only once. They share the same file list, so you don't need to worry about mixing them - a file included with require_once will not be included again by include_once. Note: include_once and require_once are statements, not functions. You don't need parentheses around the filename to be included, however you should do it anyway and use ' (apostrophes) not " (quotes): include('pre.php');

  • Tutorials
  • Login to post comments

User login

  • Request new password

Follow Us

Who's online

There are currently 0 users and 1 guest online.

Who's new

  • Nisha
  • linnaeus
  • Yameen
  • TalleyReedy
  • admin

Follow vipin7873 on Twitter

<!-- Start of Woopra Code -->

<!-- End of Woopra Code -->

  • about
  • articles
  • tutorials
  • resources
  • certification

copyright © 2010 Vipin Chandran