OSCON 2011 - Making Your PHP Application Easy to Customize

20
Making Your PHP Application Easy to Customize John Mertic @2010 SugarCRM Inc. All rights reserved.

Transcript of OSCON 2011 - Making Your PHP Application Easy to Customize

Page 1: OSCON 2011 - Making Your PHP Application Easy to Customize

Making Your PHP Application Easy to Customize

John Mertic

@2010 SugarCRM Inc. All rights reserved.

Page 2: OSCON 2011 - Making Your PHP Application Easy to Customize

Who Am I?

John Mertichttp://jmertic.wordpress.comTwitter: @[email protected] ( Work )[email protected] ( PHP )Community Manager for SugarCRM

http://www.sugarcrm.comRead our blog at http://developers.sugarcrm.com/wordpress

3/24/2011 @2011 SugarCRM Inc. All rights reserved. 2

Page 3: OSCON 2011 - Making Your PHP Application Easy to Customize

My books

3/24/2011 @2011 SugarCRM Inc. All rights reserved. 3

http://amzn.to/enioPVhttp://t.co/UFRHNSO

Page 4: OSCON 2011 - Making Your PHP Application Easy to Customize

7/22/2010 @2010 SugarCRM Inc. All rights reserved.

Why should my app be easy to customize?

4Source http://www.flickr.com/photos/duncan/4782911809

Page 5: OSCON 2011 - Making Your PHP Application Easy to Customize

So how should you do this?

Page 6: OSCON 2011 - Making Your PHP Application Easy to Customize

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 6

Make your code open

Source http://www.flickr.com/photos/igalko/4502271194

Page 7: OSCON 2011 - Making Your PHP Application Easy to Customize

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 7

Starting off with a good file structure

Page 8: OSCON 2011 - Making Your PHP Application Easy to Customize

Things good to do here

File naming conventionZend Framework approach

Class Zend_Db_Table is at "Zend/Db/Table.php”Abstract class Zend_Controller_Request_Abstract is at “Zend/Controller/Request/Abstract.php”Interface Zend_Validate_Interface is at “Zend/Validate/Interface.php”

Allowing file/class overridesSugarCRM approach

Can drop in a replacement file in the same location in the custom/ directoryExample: Override Contact’s detail view by dropping in a view.detail.php file in the custom/modules/Contacts/views/ directory.

04/13/2023 @2010 SugarCRM Inc. All rights reserved. 8

Page 9: OSCON 2011 - Making Your PHP Application Easy to Customize

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 9Source http://www.flickr.com/photos/cype_applejuice/43750657

Code Design

Page 10: OSCON 2011 - Making Your PHP Application Easy to Customize

Dependency Injection

class Widgets{ protected $_dbConn; public function __construct() { $this->_dbConn = DBFactory::getInstance(); }}

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 10

Page 11: OSCON 2011 - Making Your PHP Application Easy to Customize

Dependency Injection

class Widgets{ protected $_dbConn; public function __construct( DBInstance $dbConn ) { $this->_dbConn = $dbConn; }}

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 11

Page 12: OSCON 2011 - Making Your PHP Application Easy to Customize

Dependency Injection

class Widgets{ protected $_dbConn; public function __construct() { } public function setDBConnection(

DBInstance $dbConn ) { $this->_dbConn = $dbConn; }}

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 12

Page 13: OSCON 2011 - Making Your PHP Application Easy to Customize

Interfaces and Abstracts

interface LoggerTemplate{ /** * Main method for handling logging a message to the logger * * @param string $level logging level for the message * @param string $message */ public function log( $method, $message );}

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 13

Page 14: OSCON 2011 - Making Your PHP Application Easy to Customize

Interfaces and Abstracts

abstract class loc_xml extends source{ public function __parse($file) { $contents = file_get_contents($file); return simplexml_load_string($contents); } public abstract function getItem(

$args=array(), $module=null);

public abstract function getList($args=array(), $module=null);

}

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 14

Page 15: OSCON 2011 - Making Your PHP Application Easy to Customize

Factory Loaders

class ControllerFactory{

/** * Obtain an instance of the correct controller. * * @return an instance of SugarController */function getController($module){

$class = ucfirst($module).'Controller';$customClass = 'Custom' . $class;if(file_exists('custom/modules/'.$module.'/controller.php')){

$customClass = 'Custom' . $class;require_once('custom/modules/'.$module.'/

controller.php');if(class_exists($customClass)){

$controller = new $customClass();}else if(class_exists($class)){

$controller = new $class();}

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 15

Page 16: OSCON 2011 - Making Your PHP Application Easy to Customize

Factory Loaders

}elseif(file_exists('modules/'.$module.'/controller.php')){

require_once('modules/'.$module.'/controller.php');if(class_exists($customClass)){

$controller = new $customClass();}else if(class_exists($class)){

$controller = new $class();}

}else{if(file_exists('custom/include/MVC/Controller/

SugarController.php')){require_once('custom/include/MVC/Controller/

SugarController.php');}if(class_exists('CustomSugarController')){

$controller = new CustomSugarController();}else{$controller = new SugarController();}

}$controller->setup($module);return $controller;

}}

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 16

Page 17: OSCON 2011 - Making Your PHP Application Easy to Customize

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 17

Document it!

Source: http://www.flickr.com/photos/nicecupoftea/3218211407

Page 18: OSCON 2011 - Making Your PHP Application Easy to Customize

Ways to do this

Docblock commentsExample codeShort blog posts/articlesForums / Mailing ListWikiArchitectural documentationFull blown developer guide

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 18

Page 19: OSCON 2011 - Making Your PHP Application Easy to Customize

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 19Source: http://www.flickr.com/photos/code_martial/4145914957

Learn from your developers

Page 20: OSCON 2011 - Making Your PHP Application Easy to Customize

7/22/2010 @2010 SugarCRM Inc. All rights reserved. 20

Questions?

To contact me after my presentation, text 2OR to INTRO (46876)