standard coding, oop techniques and code reuse

31
Standard Coding, OOP Techniques and Code Reuse

Upload: rayhan-chowdhury

Post on 07-Jul-2015

6.511 views

Category:

Technology


3 download

DESCRIPTION

Its about Standard Coding Practices, Object Oriented Programming Techniques and Code Reuse tips in PHP programming language.

TRANSCRIPT

Page 1: Standard Coding, OOP Techniques and Code Reuse

Standard Coding, OOP Techniques and Code Reuse

Page 2: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 2

Standard Coding

Page 3: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 3

What isWhat is Standard CodingStandard Coding??

● Clean, smart and readable code● Follows a coding standard● Streamlined to the purpose

“Code is Art”

Page 4: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 4

Why Standard Coding?

● Code Quality● Less buggy● Maintainability● Scalability● Collaboration

(Readability)

Page 5: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 6: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 7: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 7

See, netbeans can show documentation on the fly

for you

Page 8: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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;

Page 9: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 10: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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!

Page 11: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 11

Clean and Smart

Clean and Smart

Page 12: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 12

OOP Techniques

Page 13: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 14: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 15: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 16: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 17: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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!');

Page 18: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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!');

Page 19: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 19

Order the factory to get

your favorite jeans

Factory MethodFactory Method

Page 20: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 21: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 21

class DatabaseDriver {

static function &factory($driver = null, $options = null) {

switch ($driver) {case 'postgres':

return PostgresDriver($options);

case 'sqlite':return SQLiteDriver($options);

default:return MySQLiDriver($options);

}}

}

$DbDriver = DatabaseDriver::factory('mysqli');

Page 22: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 23: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 24: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 25: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 25

Code Reuse

Page 26: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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.

Page 27: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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);

}

Page 28: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 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

Page 29: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 29

Share Your Code

● Your Blog

● PHPClasses.org

● Google Code

● Github.com

Page 30: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 30

Question and Answer

?

Page 31: Standard Coding, OOP Techniques and Code Reuse

phpXperts 2010 Md. Rayhan Chowdhury | [email protected] 31

Source

● All images are taken from flickr.com