SlideShare a Scribd company logo
1 of 37
Perttu Myry        12.11.2012




Zend
Framework
Common PHP Application
Issues, Design Patterns and
Zend Framework
Perttu Myry      12.11.2012




Zend Framework
Common issues in PHP applications

 Common     issues in PHP applications
 Useful design patterns and practices
 Introduction to Zend Framework
 Zend Framework basics
 Building a demo application
 Resources
Perttu Myry      12.11.2012




Zend Framework
Common issues in PHP applications
   Before we head into Zend Framework (or just
    ZF) let’s list some common PHP application
    architecture issues
   By definition PHP is a scripting language, it’s
    easy to do it ”wrong”
   PHP can be embedded into HTML code
   Suddenly you are doing database queries
    right next to your HTML code – a task you
    should be definitely doing in some other layer
    of your application architecture
   Everything gets tangled up
Perttu Myry      12.11.2012




Zend Framework
Common issues in PHP applications
   Debugging becomes hard work
   Maintenance and development of your
    application is a nightmare when code is not
    separated properly
   Quite often there no single entry point to your
    application which makes it hard to manage
    such operations which should be done for
    each request
   Requires and includes can become hard to
    manage
Perttu Myry      12.11.2012




Zend Framework
Useful design patterns and practices

 Common     issues in PHP applications
 Useful design patterns and practices
 Introduction to Zend Framework
 Zend Framework basics
 Building a demo application
 Resources
Perttu Myry       12.11.2012




Zend Framework
Useful design patterns and practices
   Use a version control system such as SVN or Git
   Even in single developer projects it’s useful (code
    diff, history, branches, development and
    production versions are easy to separate)
   In multiple developer environments it’s somewhat
    a necessity (see who has done what, merge
    changes, resolve conflicts)
   It’s good to know how to use highly popular Git
    (for example, if you would like to contribute to a
    project in GitHub)
Perttu Myry     12.11.2012




Zend Framework
Useful design patterns and practices

 MVC   (model-view-controller) pattern
 Most importantly separates code into
  logical structures
     Model consists of application data and
      business rules
     Controller mediates input, converting it to
      commands for the model or view
     View can be any output representation of
      data
Perttu Myry     12.11.2012




Zend Framework
Useful design patterns and practices
   This way you are not doing database queries
    embedded in your HTML code (view)
   Instead you have layer in your application for
    these kind of operations (model)
   There is a layer between view and model
    (controller) which takes care of handling user
    input to model and also handling model data
    properly to view
   Everything is properly separated, not all
    tangled up in single spaghetti code
Perttu Myry     12.11.2012




Zend Framework
Useful design patterns and practices
   There are tons of design patterns out there
   In addition to MVC I will introduce factory and
    singleton using some examples
   Factory pattern can hide complex object
    instantiation logic inside it
   Can avoid duplicate code
   One common indicator when factory pattern
    should be used can be a long if-elseif-else or
    switch-case list
Perttu Myry    12.11.2012




Zend Framework
Useful design patterns and practices
// Somewhat ugly way to instantiate a new car
if($type === 'ford') {
   return new Ford();
} else if($type === 'chevrolet') {
   return new Chevrolet();
} else if($type === 'volvo') {
   return new Volvo();
} else {
   throw new Exception('unknown car type ' . $type);
}

// Using switch-case doesn't make it any better
switch($type) {
   case 'ford':
      return new Ford();
      break;
   case 'chevrolet':
      return new Chevrolet();
      break;
   case 'volvo':
      return new Volvo();
      break;
   default:
      throw new Exception('unknown car type ' . $type);
}
Perttu Myry           12.11.2012




Zend Framework
Useful design patterns and practices
// That was yet rather simple, what if car instantiation is more
complex and you would have to do certain steps for every car you
create

// Car is a general class which is used in many places in our
application, but in this specific piece of our application we want
to make all cars have certain properties
$car = new Ford();
$car->setLocation('USA');
$car->setColor('red');

// Still that is rather simple, but already now we can do a lot
better, let's take a look how this would look like using factory
$factory = new RedCarsFactoryUSA();
$car = $factory->createCar($type);

// Once properly implemented, that’s all that it really
takes, just two lines instead of massive if-elses, let's dive bit
more into details in next couple slides
Perttu Myry        12.11.2012




Zend Framework
Useful design patterns and practices
// Simple CarFactory interface
interface CarFactory {
   public function createCar($type);
}

// CarFactory class implementing CarFactory interface
class CarFactory implements CarFactory {

    public function createCar($type) {
       // Require proper file and do it just once
       require_once 'path/to/cars/' . $type . '.php';

        // Capitalized first character will be the class name
        $class = ucfirst($type);

        // Create and return new car
        return new $class;
    }

}
Perttu Myry             12.11.2012




Zend Framework
Useful design patterns and practices
// This factory extends CarFactory which implements CarFactory interface
class RedCarsFactoryUSA extends CarFactory {

    public function createCar($type) {
       // Create new car
       $car = parent::createCar($type);

        // Set such car properties we want to have in this factory
        $car->setLocation('USA');
        $car->setColor('red');

        // Return car
        return $car;
    }

}
Perttu Myry               12.11.2012




Zend Framework
Useful design patterns and practices
// Let's also create a simple abstract Car class
abstract class Car {

    protected $location = null;
    protected $color = null;

    public function setLocation($location) {
        $this->location = $location;
    }

    public function setColor($color) {
       $this->color = $color;
    }

    public function log() {
       echo 'Car ' . ucfirst(get_class($this)) . ' is ' . $this->color
          . ' and it is located in ' . $this->location . '<br />';
    }

}
Perttu Myry                12.11.2012




Zend Framework
Useful design patterns and practices
// And some examples how to use this setup

// This code would go into path/to/cars/ford.php
class Ford extends Car { /* Ford specific code could be put here */ }

// This code would go into path/to/cars/volvo.php
class Volvo extends Car { /* Volvo specific code could be put here */ }

// Create new default factory
$defaultFactory = new CarFactory ();

// How to create cars using default factory
$car = $defaultFactory->createCar('ford');
$car->log(); // "Car Ford is NULL and it is located in NULL"
$car->setColor('blue');
$car->setLocation('Germany');
$car->log(); // "Car Ford is blue and it is located in Germany"
Perttu Myry          12.11.2012




Zend Framework
Useful design patterns and practices
// And some red car USA factory examples

$redCarsFactoryUSA = new RedCarsFactoryUSA();

$car = $redCarsFactoryUSA->createCar('ford');
$car->log(); // "Car Ford is red and it is located in USA"

$car = $redCarsFactoryUSA->createCar('volvo');
$car->log(); // "Car Volvo is red and it is located in USA“

// Ford is shipped to China...
$car->setLocation('China');
$car->log(); // "Car Volvo is red and it is located in China"
Perttu Myry      12.11.2012




Zend Framework
Useful design patterns and practices
   In singleton pattern only one instance of class
    can be initiated
   Quite common implementation to get access
    to this instance is getInstance() method
   List of singleton common uses from Wikipedia
       The Abstract Factory, Builder, and Prototype
        patterns can use Singletons in their
        implementation
       Facade Objects are often Singletons because
        only one Facade object is required
       State objects are often Singletons
Perttu Myry         12.11.2012




Zend Framework
Useful design patterns and practices
   Classic way to utilize singleton could be, for
    example, Config class
   When request is initialized, you can initialize certain
    settings in your Config, such as database host
   Later on in your script execution you want to open
    up database connection so you need to know
    what database host you should connect to
   It’s argued that better way to solve these kind of
    design issues would be to use dependency
    injection pattern, but I shall not dive into that topic
    in this presentation
Perttu Myry            12.11.2012




Zend Framework
Useful design patterns and practices
// --- Without singleton your code might look something like this

// Let's say you have this in init.php file
$config = new Config();
$config->set('db_host', '127.0.0.1');

// And this in database.php file
$config = new Config();
// This wouldn't return '127.0.0.1' (that was set in another instance)
$config->get('db_host');



// --- With singleton your PHP code could look something like this

// When you set this in init.php
Config::getInstance()->set('db_host', '127.0.0.1');

// You will always get the value from same instance
Config::getInstance()->get('db_host');
Perttu Myry              12.11.2012




Zend Framework
Useful design patterns and practices
class Config {
   private static $instance = null; // private static variable for instance
   private $settings = array(); // private variable for settings

    private function __construct() {} // private constructor

    public static function getInstance() {
       if(self::$instance === null) { // initiate only once
          self::$instance = new Config();
       }
       return self::$instance;
    }

    public function set($name, $value) {
       $this->settings[$name] = $value;
    }

    public function get($name) {
       if(array_key_exists($name, $this->settings)) {
          return $this->settings[$name];
       }
       return null;
    }
}
Perttu Myry      12.11.2012




Zend Framework
Introduction to Zend Framework

 Common     issues in PHP applications
 Useful design patterns and practices
 Introduction to Zend Framework
 Zend Framework basics
 Building a demo application
 Resources
Perttu Myry     12.11.2012




Zend Framework
Introduction to Zend Framework

 Zend,   Zend Engine, Zend Framework?
     Zend Technologies Ltd, commonly known
      as Zend, is a company
     Its founders, Andi Gutmans and Zeev
      Suraski, are key contributors to PHP and the
      creators of the core PHP scripting
      engine, the Zend Engine
     Zend Framework, or simply ZF, is an open
      source framework
Perttu Myry        12.11.2012




Zend Framework
Introduction to Zend Framework
   ”The most popular framework for modern, high-
    performing PHP applications”
    http://framework.zend.com/
   “Zend Framework 2 is an open source framework for
    developing web applications and services using PHP
    5.3+. Zend Framework 2 uses 100% object-oriented
    code and utilises most of the new features of PHP
    5.3, namely namespaces, late static binding, lambda
    functions and closures.
    Zend Framework 2 evolved from Zend Framework
    1, a successful PHP framework with over 15 million
    downloads.”
    http://framework.zend.com/about/
Perttu Myry         12.11.2012




Zend Framework
Introduction to Zend Framework
   Why use Zend Framework?
       Modular: Building blocks that can be used piece by
        piece with other applications or frameworks
       Secure: All the cryptographic and secure coding tools
        you need to do things right
       Extensible: Easy to adapt the framework to your needs
       Community: A vibrant and active contributor and user
        base for getting help and giving back
       High Performing: Engineered with performance tuning in
        mind
       Enterprise Ready: A proven history of success running
        business critical and high-usage applications
        http://framework.zend.com/
Perttu Myry      12.11.2012




Zend Framework
Introduction to Zend Framework

 And    some personal opinions
     Works as a good base for web applications
     Helps you towards good architecture
      (MVC, directory
      structure, bootstrapping, autoloading, confi
      gs etc.)
     Has lots of ready made components which
      means less work for you
     Actively developed
Perttu Myry     12.11.2012




Zend Framework
Introduction to Zend Framework
 In this presentation I’m going to focus on
  ZF 1 for couple reasons
      ZF 2 was released in September 2012, just
       two months ago
      I have personal experience only from ZF 1
      A lot has changed in ZF 2
 Ifyou are looking into choosing ZF as base
  for you web application, I suggest you
  take a look at ZF 2
Perttu Myry      12.11.2012




Zend Framework
Zend Framework application basics

 Common     issues in PHP applications
 Useful design patterns and practices
 Introduction to Zend Framework
 Zend Framework basics
 Building a demo application
 Resources
Perttu Myry                12.11.2012




Zend Framework
                                        <project name>
                                        |-- application
                                        | |-- Bootstrap.php

Zend Framework basics                   | |-- configs
                                        | | `-- application.ini
                                        | |-- controllers
                                        | | |-- ErrorController.php
 First we are going to take            | | `-- IndexController.php
  a look at proposed                    | |-- models
                                        | `-- views
  directory structure for ZF            |
                                        |
                                               |-- helpers
                                               `-- scripts
  project                               |
                                        |
                                                  |-- error
                                                  | `-- error.phtml
 At first glance that                  |
                                        |
                                                  `-- index
                                                     `-- index.phtml
  doesn’t look too                      |-- library
                                        |-- public
  bad, let’s get into details           | |-- .htaccess
                                        | `-- index.php
  why all these folders and             `-- tests
                                           |-- application
  files are here                           | `-- bootstrap.php
                                           |-- library
                                           | `-- bootstrap.php
                                           `-- phpunit.xml
Perttu Myry     12.11.2012




Zend Framework
Zend Framework application basics

 Bootstrapping
     Zend Framework’s controller uses the Front
      Controller design pattern
     Routes all requests through a single
      index.php file
     Ensures that the environment is set up
      correctly for running the application
Perttu Myry       12.11.2012




Zend Framework
Zend Framework application basics
   URL structure
       ZF uses forward slash (/) as URL separator
       After base URL your ZF application consists of
        module, controller and action parameters
       If not defined ZF will use default value which is
        “default” for modules and “index” for controllers
        and actions
       After that URL consists of /param/value pairs
        (corresponds to &param=value URL structure)
Perttu Myry        12.11.2012




Zend Framework
Zend Framework application basics
   Some URL examples
       Here base means the URL to your application
        (public folder), such as http://localhost/zfdemo/
       base: default module, controller and action (same
        as base/default/index/index)
       base/foo/bar: in case foo is not a module, refers
        to default module’s foo controller’s bar action
       base/foo/bar: in case foo is a module, refers to
        foo module’s bar controller’s default action (if foo
        would be both controller and module name, you
        could refer to default module’s foo controller by
        base/default/foo/bar)
       base/default/foo/bar/id/123/name/john: default
        module’s foo controller’s bar action gets two
        request parameters “id” which value is “123” and
        “name” which value is “john”
Perttu Myry      12.11.2012




Zend Framework
Zend Framework application basics

 Autoloader
     PHP 5 introduced autoloading
     Zend Framework has its own autoloader
      which is based on this autoloading
     This means you don’t have to write any
      include or require statements (unless you
      are using a class or interface which is
      located outside your autoloading
      definitions)
Perttu Myry      12.11.2012




Zend Framework
Building a demo application

 Common     issues in PHP applications
 Useful design patterns and practices
 Introduction to Zend Framework
 Zend Framework basics
 Building a demo application
 Resources
Perttu Myry   12.11.2012




Zend Framework
Building a demo application

 Let’sgo through the Zend Framework 1
  Getting started guide
Perttu Myry      12.11.2012




Zend Framework
Resources

 Common     issues in PHP applications
 Useful design patterns and practices
 Introduction to Zend Framework
 Zend Framework basics
 Building a demo application
 Resources
Perttu Myry   12.11.2012




Resources
Books

 ZendFramework in Action
 Head First Design Patterns
Perttu Myry       12.11.2012




Resources
Links

 Getting   started with Zend Framework
  tutorial by Rob Allen
 Apache mod_rewrite reference
  documentation

More Related Content

What's hot

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web FrameworkLuther Baker
 
[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법Jeado Ko
 
Building an HTML5 Video Player
Building an HTML5 Video PlayerBuilding an HTML5 Video Player
Building an HTML5 Video PlayerJim Jeffers
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptLaurence Svekis ✔
 
Web internship Yii Framework
Web internship  Yii FrameworkWeb internship  Yii Framework
Web internship Yii FrameworkNoveo
 
Web前端标准在各浏览器中的实现差异
Web前端标准在各浏览器中的实现差异Web前端标准在各浏览器中的实现差异
Web前端标准在各浏览器中的实现差异Open Party
 
Magento Product Types Demystified
Magento Product Types DemystifiedMagento Product Types Demystified
Magento Product Types DemystifiedAOE
 
Boston Computing Review - Java Server Pages
Boston Computing Review - Java Server PagesBoston Computing Review - Java Server Pages
Boston Computing Review - Java Server PagesJohn Brunswick
 
Angularjs practical project experiences with javascript development in a bank
Angularjs practical project experiences with javascript development in a bankAngularjs practical project experiences with javascript development in a bank
Angularjs practical project experiences with javascript development in a bankDavid Amend
 
Angular JS2 Training Session #1
Angular JS2 Training Session #1Angular JS2 Training Session #1
Angular JS2 Training Session #1Paras Mendiratta
 
Vaadin Components @ Angular U
Vaadin Components @ Angular UVaadin Components @ Angular U
Vaadin Components @ Angular UJoonas Lehtinen
 

What's hot (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Rails + Webpack
Rails + WebpackRails + Webpack
Rails + Webpack
 
tut0000021-hevery
tut0000021-heverytut0000021-hevery
tut0000021-hevery
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web Framework
 
[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법
 
Building an HTML5 Video Player
Building an HTML5 Video PlayerBuilding an HTML5 Video Player
Building an HTML5 Video Player
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Web internship Yii Framework
Web internship  Yii FrameworkWeb internship  Yii Framework
Web internship Yii Framework
 
Wt unit 2 ppts client side technology
Wt unit 2 ppts client side technologyWt unit 2 ppts client side technology
Wt unit 2 ppts client side technology
 
Web前端标准在各浏览器中的实现差异
Web前端标准在各浏览器中的实现差异Web前端标准在各浏览器中的实现差异
Web前端标准在各浏览器中的实现差异
 
Wt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side frameworkWt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side framework
 
Wt unit 6 ppts web services
Wt unit 6 ppts web servicesWt unit 6 ppts web services
Wt unit 6 ppts web services
 
Magento Product Types Demystified
Magento Product Types DemystifiedMagento Product Types Demystified
Magento Product Types Demystified
 
Patterns In-Javascript
Patterns In-JavascriptPatterns In-Javascript
Patterns In-Javascript
 
Boston Computing Review - Java Server Pages
Boston Computing Review - Java Server PagesBoston Computing Review - Java Server Pages
Boston Computing Review - Java Server Pages
 
Angularjs practical project experiences with javascript development in a bank
Angularjs practical project experiences with javascript development in a bankAngularjs practical project experiences with javascript development in a bank
Angularjs practical project experiences with javascript development in a bank
 
Angular JS2 Training Session #1
Angular JS2 Training Session #1Angular JS2 Training Session #1
Angular JS2 Training Session #1
 
JavaScript Basics with baby steps
JavaScript Basics with baby stepsJavaScript Basics with baby steps
JavaScript Basics with baby steps
 
Vaadin Components @ Angular U
Vaadin Components @ Angular UVaadin Components @ Angular U
Vaadin Components @ Angular U
 

Similar to Zend Framework

Foundations of Zend Framework
Foundations of Zend FrameworkFoundations of Zend Framework
Foundations of Zend FrameworkAdam Culp
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practicesmanugoel2003
 
Zend Framework 1.8 Features Webinar
Zend Framework 1.8 Features WebinarZend Framework 1.8 Features Webinar
Zend Framework 1.8 Features WebinarRalph Schindler
 
A quick start on Zend Framework 2
A quick start on Zend Framework 2A quick start on Zend Framework 2
A quick start on Zend Framework 2Enrico Zimuel
 
Zend_Layout & Zend_View Enhancements
Zend_Layout & Zend_View EnhancementsZend_Layout & Zend_View Enhancements
Zend_Layout & Zend_View EnhancementsRalph Schindler
 
Magento - a Zend Framework Application
Magento - a Zend Framework ApplicationMagento - a Zend Framework Application
Magento - a Zend Framework ApplicationZendCon
 
ZF2 Modular Architecture - Taking advantage of it
ZF2 Modular Architecture - Taking advantage of itZF2 Modular Architecture - Taking advantage of it
ZF2 Modular Architecture - Taking advantage of itSteve Maraspin
 
Zend_Tool: Rapid Application Development with Zend Framework
Zend_Tool: Rapid Application Development with Zend FrameworkZend_Tool: Rapid Application Development with Zend Framework
Zend_Tool: Rapid Application Development with Zend FrameworkRalph Schindler
 
Using Zend_Tool to Establish Your Project's Skeleton
Using Zend_Tool to Establish Your Project's SkeletonUsing Zend_Tool to Establish Your Project's Skeleton
Using Zend_Tool to Establish Your Project's SkeletonJeremy Brown
 
Unit Test for ZF SlideShare Component
Unit Test for ZF SlideShare ComponentUnit Test for ZF SlideShare Component
Unit Test for ZF SlideShare Componentzftalk
 

Similar to Zend Framework (20)

Foundations of Zend Framework
Foundations of Zend FrameworkFoundations of Zend Framework
Foundations of Zend Framework
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Zend Framework 1.8 Features Webinar
Zend Framework 1.8 Features WebinarZend Framework 1.8 Features Webinar
Zend Framework 1.8 Features Webinar
 
A quick start on Zend Framework 2
A quick start on Zend Framework 2A quick start on Zend Framework 2
A quick start on Zend Framework 2
 
Zend_Layout & Zend_View Enhancements
Zend_Layout & Zend_View EnhancementsZend_Layout & Zend_View Enhancements
Zend_Layout & Zend_View Enhancements
 
Magento - a Zend Framework Application
Magento - a Zend Framework ApplicationMagento - a Zend Framework Application
Magento - a Zend Framework Application
 
ZF2 Modular Architecture - Taking advantage of it
ZF2 Modular Architecture - Taking advantage of itZF2 Modular Architecture - Taking advantage of it
ZF2 Modular Architecture - Taking advantage of it
 
Zend
ZendZend
Zend
 
Zend_Tool: Rapid Application Development with Zend Framework
Zend_Tool: Rapid Application Development with Zend FrameworkZend_Tool: Rapid Application Development with Zend Framework
Zend_Tool: Rapid Application Development with Zend Framework
 
Demo
DemoDemo
Demo
 
Using Zend_Tool to Establish Your Project's Skeleton
Using Zend_Tool to Establish Your Project's SkeletonUsing Zend_Tool to Establish Your Project's Skeleton
Using Zend_Tool to Establish Your Project's Skeleton
 
first pitch
first pitchfirst pitch
first pitch
 
werwr
werwrwerwr
werwr
 
sdfsdf
sdfsdfsdfsdf
sdfsdf
 
college
collegecollege
college
 
first pitch
first pitchfirst pitch
first pitch
 
Greenathan
GreenathanGreenathan
Greenathan
 
Unit Test for ZF SlideShare Component
Unit Test for ZF SlideShare ComponentUnit Test for ZF SlideShare Component
Unit Test for ZF SlideShare Component
 
first pitch
first pitchfirst pitch
first pitch
 
organic
organicorganic
organic
 

Zend Framework

  • 1. Perttu Myry 12.11.2012 Zend Framework Common PHP Application Issues, Design Patterns and Zend Framework
  • 2. Perttu Myry 12.11.2012 Zend Framework Common issues in PHP applications  Common issues in PHP applications  Useful design patterns and practices  Introduction to Zend Framework  Zend Framework basics  Building a demo application  Resources
  • 3. Perttu Myry 12.11.2012 Zend Framework Common issues in PHP applications  Before we head into Zend Framework (or just ZF) let’s list some common PHP application architecture issues  By definition PHP is a scripting language, it’s easy to do it ”wrong”  PHP can be embedded into HTML code  Suddenly you are doing database queries right next to your HTML code – a task you should be definitely doing in some other layer of your application architecture  Everything gets tangled up
  • 4. Perttu Myry 12.11.2012 Zend Framework Common issues in PHP applications  Debugging becomes hard work  Maintenance and development of your application is a nightmare when code is not separated properly  Quite often there no single entry point to your application which makes it hard to manage such operations which should be done for each request  Requires and includes can become hard to manage
  • 5. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices  Common issues in PHP applications  Useful design patterns and practices  Introduction to Zend Framework  Zend Framework basics  Building a demo application  Resources
  • 6. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices  Use a version control system such as SVN or Git  Even in single developer projects it’s useful (code diff, history, branches, development and production versions are easy to separate)  In multiple developer environments it’s somewhat a necessity (see who has done what, merge changes, resolve conflicts)  It’s good to know how to use highly popular Git (for example, if you would like to contribute to a project in GitHub)
  • 7. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices  MVC (model-view-controller) pattern  Most importantly separates code into logical structures  Model consists of application data and business rules  Controller mediates input, converting it to commands for the model or view  View can be any output representation of data
  • 8. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices  This way you are not doing database queries embedded in your HTML code (view)  Instead you have layer in your application for these kind of operations (model)  There is a layer between view and model (controller) which takes care of handling user input to model and also handling model data properly to view  Everything is properly separated, not all tangled up in single spaghetti code
  • 9. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices  There are tons of design patterns out there  In addition to MVC I will introduce factory and singleton using some examples  Factory pattern can hide complex object instantiation logic inside it  Can avoid duplicate code  One common indicator when factory pattern should be used can be a long if-elseif-else or switch-case list
  • 10. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices // Somewhat ugly way to instantiate a new car if($type === 'ford') { return new Ford(); } else if($type === 'chevrolet') { return new Chevrolet(); } else if($type === 'volvo') { return new Volvo(); } else { throw new Exception('unknown car type ' . $type); } // Using switch-case doesn't make it any better switch($type) { case 'ford': return new Ford(); break; case 'chevrolet': return new Chevrolet(); break; case 'volvo': return new Volvo(); break; default: throw new Exception('unknown car type ' . $type); }
  • 11. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices // That was yet rather simple, what if car instantiation is more complex and you would have to do certain steps for every car you create // Car is a general class which is used in many places in our application, but in this specific piece of our application we want to make all cars have certain properties $car = new Ford(); $car->setLocation('USA'); $car->setColor('red'); // Still that is rather simple, but already now we can do a lot better, let's take a look how this would look like using factory $factory = new RedCarsFactoryUSA(); $car = $factory->createCar($type); // Once properly implemented, that’s all that it really takes, just two lines instead of massive if-elses, let's dive bit more into details in next couple slides
  • 12. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices // Simple CarFactory interface interface CarFactory { public function createCar($type); } // CarFactory class implementing CarFactory interface class CarFactory implements CarFactory { public function createCar($type) { // Require proper file and do it just once require_once 'path/to/cars/' . $type . '.php'; // Capitalized first character will be the class name $class = ucfirst($type); // Create and return new car return new $class; } }
  • 13. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices // This factory extends CarFactory which implements CarFactory interface class RedCarsFactoryUSA extends CarFactory { public function createCar($type) { // Create new car $car = parent::createCar($type); // Set such car properties we want to have in this factory $car->setLocation('USA'); $car->setColor('red'); // Return car return $car; } }
  • 14. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices // Let's also create a simple abstract Car class abstract class Car { protected $location = null; protected $color = null; public function setLocation($location) { $this->location = $location; } public function setColor($color) { $this->color = $color; } public function log() { echo 'Car ' . ucfirst(get_class($this)) . ' is ' . $this->color . ' and it is located in ' . $this->location . '<br />'; } }
  • 15. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices // And some examples how to use this setup // This code would go into path/to/cars/ford.php class Ford extends Car { /* Ford specific code could be put here */ } // This code would go into path/to/cars/volvo.php class Volvo extends Car { /* Volvo specific code could be put here */ } // Create new default factory $defaultFactory = new CarFactory (); // How to create cars using default factory $car = $defaultFactory->createCar('ford'); $car->log(); // "Car Ford is NULL and it is located in NULL" $car->setColor('blue'); $car->setLocation('Germany'); $car->log(); // "Car Ford is blue and it is located in Germany"
  • 16. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices // And some red car USA factory examples $redCarsFactoryUSA = new RedCarsFactoryUSA(); $car = $redCarsFactoryUSA->createCar('ford'); $car->log(); // "Car Ford is red and it is located in USA" $car = $redCarsFactoryUSA->createCar('volvo'); $car->log(); // "Car Volvo is red and it is located in USA“ // Ford is shipped to China... $car->setLocation('China'); $car->log(); // "Car Volvo is red and it is located in China"
  • 17. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices  In singleton pattern only one instance of class can be initiated  Quite common implementation to get access to this instance is getInstance() method  List of singleton common uses from Wikipedia  The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation  Facade Objects are often Singletons because only one Facade object is required  State objects are often Singletons
  • 18. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices  Classic way to utilize singleton could be, for example, Config class  When request is initialized, you can initialize certain settings in your Config, such as database host  Later on in your script execution you want to open up database connection so you need to know what database host you should connect to  It’s argued that better way to solve these kind of design issues would be to use dependency injection pattern, but I shall not dive into that topic in this presentation
  • 19. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices // --- Without singleton your code might look something like this // Let's say you have this in init.php file $config = new Config(); $config->set('db_host', '127.0.0.1'); // And this in database.php file $config = new Config(); // This wouldn't return '127.0.0.1' (that was set in another instance) $config->get('db_host'); // --- With singleton your PHP code could look something like this // When you set this in init.php Config::getInstance()->set('db_host', '127.0.0.1'); // You will always get the value from same instance Config::getInstance()->get('db_host');
  • 20. Perttu Myry 12.11.2012 Zend Framework Useful design patterns and practices class Config { private static $instance = null; // private static variable for instance private $settings = array(); // private variable for settings private function __construct() {} // private constructor public static function getInstance() { if(self::$instance === null) { // initiate only once self::$instance = new Config(); } return self::$instance; } public function set($name, $value) { $this->settings[$name] = $value; } public function get($name) { if(array_key_exists($name, $this->settings)) { return $this->settings[$name]; } return null; } }
  • 21. Perttu Myry 12.11.2012 Zend Framework Introduction to Zend Framework  Common issues in PHP applications  Useful design patterns and practices  Introduction to Zend Framework  Zend Framework basics  Building a demo application  Resources
  • 22. Perttu Myry 12.11.2012 Zend Framework Introduction to Zend Framework  Zend, Zend Engine, Zend Framework?  Zend Technologies Ltd, commonly known as Zend, is a company  Its founders, Andi Gutmans and Zeev Suraski, are key contributors to PHP and the creators of the core PHP scripting engine, the Zend Engine  Zend Framework, or simply ZF, is an open source framework
  • 23. Perttu Myry 12.11.2012 Zend Framework Introduction to Zend Framework  ”The most popular framework for modern, high- performing PHP applications” http://framework.zend.com/  “Zend Framework 2 is an open source framework for developing web applications and services using PHP 5.3+. Zend Framework 2 uses 100% object-oriented code and utilises most of the new features of PHP 5.3, namely namespaces, late static binding, lambda functions and closures. Zend Framework 2 evolved from Zend Framework 1, a successful PHP framework with over 15 million downloads.” http://framework.zend.com/about/
  • 24. Perttu Myry 12.11.2012 Zend Framework Introduction to Zend Framework  Why use Zend Framework?  Modular: Building blocks that can be used piece by piece with other applications or frameworks  Secure: All the cryptographic and secure coding tools you need to do things right  Extensible: Easy to adapt the framework to your needs  Community: A vibrant and active contributor and user base for getting help and giving back  High Performing: Engineered with performance tuning in mind  Enterprise Ready: A proven history of success running business critical and high-usage applications http://framework.zend.com/
  • 25. Perttu Myry 12.11.2012 Zend Framework Introduction to Zend Framework  And some personal opinions  Works as a good base for web applications  Helps you towards good architecture (MVC, directory structure, bootstrapping, autoloading, confi gs etc.)  Has lots of ready made components which means less work for you  Actively developed
  • 26. Perttu Myry 12.11.2012 Zend Framework Introduction to Zend Framework  In this presentation I’m going to focus on ZF 1 for couple reasons  ZF 2 was released in September 2012, just two months ago  I have personal experience only from ZF 1  A lot has changed in ZF 2  Ifyou are looking into choosing ZF as base for you web application, I suggest you take a look at ZF 2
  • 27. Perttu Myry 12.11.2012 Zend Framework Zend Framework application basics  Common issues in PHP applications  Useful design patterns and practices  Introduction to Zend Framework  Zend Framework basics  Building a demo application  Resources
  • 28. Perttu Myry 12.11.2012 Zend Framework <project name> |-- application | |-- Bootstrap.php Zend Framework basics | |-- configs | | `-- application.ini | |-- controllers | | |-- ErrorController.php  First we are going to take | | `-- IndexController.php a look at proposed | |-- models | `-- views directory structure for ZF | | |-- helpers `-- scripts project | | |-- error | `-- error.phtml  At first glance that | | `-- index `-- index.phtml doesn’t look too |-- library |-- public bad, let’s get into details | |-- .htaccess | `-- index.php why all these folders and `-- tests |-- application files are here | `-- bootstrap.php |-- library | `-- bootstrap.php `-- phpunit.xml
  • 29. Perttu Myry 12.11.2012 Zend Framework Zend Framework application basics  Bootstrapping  Zend Framework’s controller uses the Front Controller design pattern  Routes all requests through a single index.php file  Ensures that the environment is set up correctly for running the application
  • 30. Perttu Myry 12.11.2012 Zend Framework Zend Framework application basics  URL structure  ZF uses forward slash (/) as URL separator  After base URL your ZF application consists of module, controller and action parameters  If not defined ZF will use default value which is “default” for modules and “index” for controllers and actions  After that URL consists of /param/value pairs (corresponds to &param=value URL structure)
  • 31. Perttu Myry 12.11.2012 Zend Framework Zend Framework application basics  Some URL examples  Here base means the URL to your application (public folder), such as http://localhost/zfdemo/  base: default module, controller and action (same as base/default/index/index)  base/foo/bar: in case foo is not a module, refers to default module’s foo controller’s bar action  base/foo/bar: in case foo is a module, refers to foo module’s bar controller’s default action (if foo would be both controller and module name, you could refer to default module’s foo controller by base/default/foo/bar)  base/default/foo/bar/id/123/name/john: default module’s foo controller’s bar action gets two request parameters “id” which value is “123” and “name” which value is “john”
  • 32. Perttu Myry 12.11.2012 Zend Framework Zend Framework application basics  Autoloader  PHP 5 introduced autoloading  Zend Framework has its own autoloader which is based on this autoloading  This means you don’t have to write any include or require statements (unless you are using a class or interface which is located outside your autoloading definitions)
  • 33. Perttu Myry 12.11.2012 Zend Framework Building a demo application  Common issues in PHP applications  Useful design patterns and practices  Introduction to Zend Framework  Zend Framework basics  Building a demo application  Resources
  • 34. Perttu Myry 12.11.2012 Zend Framework Building a demo application  Let’sgo through the Zend Framework 1 Getting started guide
  • 35. Perttu Myry 12.11.2012 Zend Framework Resources  Common issues in PHP applications  Useful design patterns and practices  Introduction to Zend Framework  Zend Framework basics  Building a demo application  Resources
  • 36. Perttu Myry 12.11.2012 Resources Books  ZendFramework in Action  Head First Design Patterns
  • 37. Perttu Myry 12.11.2012 Resources Links  Getting started with Zend Framework tutorial by Rob Allen  Apache mod_rewrite reference documentation