3. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 3
What isWhat is Standard CodingStandard Coding??
● Clean, smart and readable code
● Follows a coding standard
● Streamlined to the purpose
“Code is Art”
5. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 5
Coding Standards
● Zend Coding Standard
- The most advanced coding standard in PHP
● PEAR Coding Standards
- Base for most of the coding standards in PHP
● CakePHP Coding Standard
● CodeIgniter Coding Standard
Note: There are tons of bad & inconsistent coding standards in
PHP
6. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 6
Code Commenting
● Remember! Doc-block is must for File, Class and Method
● Always in phpDocumentor format, don't put your own tag
● Latest PHP IDE like NetBeans, Zend Studio, Eclipse PDT
can help you generating doc-block easily
● Benefit
● Documentation is done as you go with coding
● Doxygen can build your API documentation in chm
format instantly from your code
● IDE like netbeans shows code hints, code completion
along with popup documentation on the fly
7. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 7
See, netbeans can show
documentation on the fly
for you
8. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 8
Standard Coding - Tips
● Say no to short tags <? ?>, always long tags (<?php ?>)
● Use foreach instead of for loop for array iteration
● Ensure variable is not empty before any iteration
● Use null as default function parameter if possible
● Avoid nested conditions, return as early as possible
● Use alternative syntax for control structures when mixing
with HTML
if ():
else:
endif;
foreach ():
endforeach;
for ():
endfor;
while ():
endwhile;
9. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 9
Standard Coding – empty
●
empty() is a magic in php, learn how to use it, at
the end you will love it
● Use !empty() instead of isset() when working
with non-empty or non-zero value
● Use !empty($array) instead of count($array)
when checking if array has value
● Always check your variable before going further with
it
10. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 10
array functions = green power
● Learn them all, specially: in_array,
array_merge, array_map,
array_values, array_keys, join,
explode!
● They can save your time!
11. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 11
Clean and Smart
Clean and Smart
13. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 13
Static Method & Variables
● Ever used Configure::read() in CakePHP.
● Remember! Public static method can be accessed
from any part of your application
● No more global function, move them to a wrapper
class with static method
● All methods in Sanitize, Set and String among other
classes in CakePHP are static
● Extensively used in Zend Framework too
14. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 14
One-time/Magic Configuration
● Tired of multiple setters! Or you can't remember the
order of parameters
● $options array as function parameter instead of
multiple parameters or multiple setters
● setConfig() method, in constructor call
setConfig(), do not bypass.
● Ensure configure and reconfigure ability
● Chain your method if possible
15. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 15
Design PatternsDesign Patterns
●
Solve common design problems not coding problems
● Singleton, Factory and MVC patterns are common
● Worth learning: Registry, Adapter, Decorator, Observer and
Strategy patterns
● Same task can be done using different patterns, so choose
wisely!
● Reminder! Patterns are not perfect, they have week points
too
16. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 16
Singleton - Design Pattern
●
Ensure a class has only one instance
● Provide a global point of access to it
● Mostly used in application level Configuration,
Logging, Database etc
● CakePHP uses extensive number of singleton
classes like Configure, Router, App with static
method etc
● Lazy initialization
17. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 17
class Logger {
protected static $_instance;
private function __construct($options = null) {}
public static function &getInstance($options = null)
{
If (is_null(self::$_instance)) {
self::$_instance = new self($options);
}
return self::$_instance;
}
public function write($message, $type = null) {}
}
Logger::getInstance()->write('My log message');
// Or
$Logger = Logger::getInstance();
$Logger->write('My log message!');
18. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 18
class Logger {
protected static $_instance;
private function __construct($options = null) {}
public static function &getInstance($options = null) {
if (is_null(self::$_instance)) {
self::$_instance = new self($options);
}
return self::$_instance;
}
protected function _write($message, $type = null) {}
public static function write($message, $type = null) {
$_this = self::getInstance();
$_this->_write($message, $type = null);
}
}
Logger::write('My log message!');
19. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 19
Order the factory
to get
your favorite jeans
Factory MethodFactory Method
20. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 20
Factory Method - Design Pattern
● Single interface for creating instance from a family of
classes that can do the same task in multiple ways
● Reduce platform dependency
● Example:
● Database Driver
– mysqli, sqlite, oracle, postgres, odbc, mssql
● Authentication
– Database, LDAP, OpenID etc
22. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 22
MVC - Design Patterns
● Separate presentation, decision making and data
layer
● Web is MVC
● So, your web application should be
● Zend Framework, CakePHP, CodeIgniter all are
MVC
StorageClient Server
23. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 23
ORM – Object Relational Mapping
● Raw SQL? No more!
● ORM is more flexible, efficient and maintainable
● CakePHP has built-in ORM through association
● Zend has Relationships using Zend_Db_Table
● Doctrine and Propel are most popular ORM in PHP
● Doctrine can be used with both Zend Framework &
CodeIgniter
24. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 24
Abstract Class
● Incomplete implementation of some concept
● You can implement multiple Interfaces but extend
from only one abstract class
● You can't create instance of an Abstract Class
● Type hinting, enforce object type
26. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 26
Code Reuse – when and how?
● Don't Re-Invent the Wheel
● But, you can always Improve the Wheel
● Decide when to Reuse
● Got a peace of code that can perform a specific task
individually
● No copy & paste of the same code, put them inside a
wrapper class and call whenever you need.
27. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 27
Wrap your code
● Say you have got a
piece of code for
updating Twitter
Status
● First, separate
configuration variable
like apiUrl,
username and
password
● Separate variables
that are specific to
call, like message
● Write your update
method
class Twitter {
protected $_configs = array(
'apiUrl' => '_api_url_',
'username' => '',
'password' => ''
);
function __construct($options)
{
$this->_configs =
$options; }
function setConfig($options);
function update($message);
}
28. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 28
Avoid DependencyAvoid Dependency
● My belief “Always keep the door open”
● Loosely coupled classes are reusable
● Make your class independent and flexible as much
as you can
● Avoid dependency on global variables,
constants, files, paths, system parameters
● Use dependency injection if necessary
29. phpXperts 2010 Md. Rayhan Chowdhury | ray@raynux.com 29
Share Your Code
● Your Blog
● PHPClasses.org
● Google Code
● Github.com