SlideShare a Scribd company logo
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

PPTX
Modern Web Technologies
PDF
MVC in PHP
PDF
Javascript Best Practices
PDF
IOC + Javascript
PDF
PHP & MVC
PDF
當ZK遇見Front-End
PPT
Seam Introduction
ODP
Wicket Next (1.4/1.5)
Modern Web Technologies
MVC in PHP
Javascript Best Practices
IOC + Javascript
PHP & MVC
當ZK遇見Front-End
Seam Introduction
Wicket Next (1.4/1.5)

What's hot (20)

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

Similar to Zend Framework (20)

PPTX
E-Commerce Applications Development
PPT
5 Design Patterns Explained
PDF
ZF2 Presentation @PHP Tour 2011 in Lille
PPTX
Zend MVC pattern based Framework – Best for Enterprise web applications
PDF
Your first 5 PHP design patterns - ThatConference 2012
PPTX
Gof design patterns
PDF
Design patterns
PDF
Design Patterns in PHP
PDF
10 PHP Design Patterns #burningkeyboards
PDF
Zend Framework 2 quick start
PPT
Software Design Patterns
PDF
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
PPT
Software Design Patterns
PPT
Software Design Patterns
KEY
Extending ZF & Extending With ZF
PDF
Software Patterns
PPTX
Design patterns as power of programing
PDF
Design attern in php
PPTX
OOPSDesign PPT ( introduction to opps and design (
PDF
PHP: 4 Design Patterns to Make Better Code
E-Commerce Applications Development
5 Design Patterns Explained
ZF2 Presentation @PHP Tour 2011 in Lille
Zend MVC pattern based Framework – Best for Enterprise web applications
Your first 5 PHP design patterns - ThatConference 2012
Gof design patterns
Design patterns
Design Patterns in PHP
10 PHP Design Patterns #burningkeyboards
Zend Framework 2 quick start
Software Design Patterns
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
Software Design Patterns
Software Design Patterns
Extending ZF & Extending With ZF
Software Patterns
Design patterns as power of programing
Design attern in php
OOPSDesign PPT ( introduction to opps and design (
PHP: 4 Design Patterns to Make Better Code
Ad

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