Object Oriented Programming in PHP by ebd21054


									Object Oriented Programming in

     A framework for KEWL.NextGen
          Structure of workshop
●   Introduction
●   KEWL.NextGen framework overview
●   The Model/View/Controller design pattern
●   Writing KEWL.NextGen modules
●   Final words
●   Me
●   My apps / history of PHP framework
●   Framework not finished work!
●   PHP has limitations
    KEWL framework overview

An architectural overview of the object-oriented PHP
            framework for KEWL.NextGen
          Aims of the framework
●   Easier to write modules
    (less code)
●   Easier to maintain modules
    (less boilerplate code)
●   Separation of areas of responsibility
    (contributes to both of above)
           Roles of $objEngine
●   Load and run module controllers
●   Provide services to modules (object loading,
    template variables, generating URIs)
●   Manage display flow (content and layout
      $objEngine: module loading
●   Module is selected by the 'module' URI query
                          ht t p://kngforge.uwc.ac.za/index.php?m odule= forum

●   Engine loads PHP script
●   Engine creates an object of class
●   Engine calls dispatch method of object
           $objEngine: services
●   Class loading/object caching (getObject
●   Generating URIs (uri method)
                                   push/preserve/pop m echanism
●   Handling general authentication and
●   Handling session and template variables
          $objEngine: display flow
●   Content template contains
    page-specific content          Layout template

●   Layout template contains
    common navigational
                                   Content template
●   Content template rendered
    first, can specify non-
    default layout template plus
    page title etc
   MVC Design Pattern

The Model/View/Controller design pattern
               MVC Overview
●   MVC term was coined by inventors of
    SmallTalk, regarded by many as the original
    object-oriented language
●   Involves separation of an interactive
    application into three layers, model (the
    data), view (how the data is displayed) and
    controller (how the user interacts with the
          MVC overview (cont)
●   Several MVC frameworks for building
    traditional GUI applications (NeXTSTEP,
    MFC, wxWidgets)
●   Now perhaps even more popular as web
    architecture (Turbine, Struts, PHPMVC)
●   Also known as “model 2” especially in the
    Java world (classic ASP/PHP/JSP = model
          MVC Overview (cont 2)
●   MVC is a controversial term, with many
    conflicting views on exactly what constitutes
    a 'real' MVC architecture
●   Often hazy areas at the edges of layers
    where it is not clear into which layer a
    particular piece of code/functionality falls.
●   Guidelines not rules!
                    Why MVC?
●   Separates out both view and model logic, so
    they can be worked on independently (even if
    by same person)
●   Separates out layers by likelihood of change:
    view changes often; controller less often; and
    model relatively rarely.
●   The architecture is a good fit for web
    application flow      request -> processing ->     response

                               cont roller -> m odel -> v iew
             MVC: The Model
●   The model layer in MVC encapsulates the
    underlying data of the application and
    operations upon it.
●   In web apps usually an abstraction of access
    to the database
●   In the KEWL.NextGen framework, model
    objects usually extend 'dbTable'
              MVC: The View
●   The view layer encapsulates logic to display
    data to the user
●   In a web app usually implemented by display
●   In PHP the 'Smarty' template system is often
    used for this layer.
●   In KEWL.NextGen we are currently using
    PHP scripts as templates.
           MVC: The Controller
●   The controller layer handles user
    manipulation of the model
●   In some ways mediates between view and
    model (controversial)
●   In a web app handles form and URI
    parameters, manipulation of model (updating,
    filtering) and choice of view template
      Writing a module

Step-by-step guide to writing a module for
              Writing a module
●   Not covered: module registration and
●   Directory layout
●   Data and helper objects
●   The controller class
●   Content templates
                   Directory layout
–   modules/
     ●   controller.php
     ●   classes/
     ●   templates/
          –   content
          –   layout
     ●   register.php
     ●   uninstall.php
     ●   admin_controller.php
     ●   webservice_controller.php
         Data and helper objects
●   Defined in file named
●   Loaded by own module via:
    or from other module via
     $this->getObject(“CLASSNAME”, “MODULE”)
           Model (data) objects
●   Derived from dbTable
●   Encapsulate details of data structure – shield
    other layers from those details
●   Should be usable in any context so...
●   Data (model) objects should not contain any
    HTML or code specific to the web
                Helper objects
●   Broad category, any additional logic that it
    makes sense to split off
●   Can provide service to controller, model or
    view but only one of the three
●   Many currently core classes can become
    helper or model objects
           Helper object examples
●   Model
    –   calculation and analysis routines
●   View
    –   controls (buttons, dropdowns, menus etc)
●   Controller
    –   form validation
             The controller class
●   Inherits from (PHP=extends) class 'controller'
●   Initialised via init() method
●   Asked to process a web request via
    dispatch() method
●   dispatch() returns name of content template
●   Access to engine services via baseclass
       The controller class (cont)
●   Request (query and form) parameters should
    be retrieved by calling
●   Session variables should be set/retrieved by
    $this->getSession() and $this->putSession()
●   Errors by calling
               Controller example

class helloworld extends controller
       var $objLanguage;

       function init()
              $this->objLanguage =& $this->getObject('language',

       function dispatch($action)
              switch ($action)
                     case 'dosomethingelse':
                            return $this->do_somethingelse();
                     case 'showhello':
                            return $this->do_showhello();

       function do_showhello($action)
              $this->setVarByRef('objLanguage', $this->objLanguage);
              $this->setVar('name', “Sean”);
              return 'hello_tpl.php';
            Template selection
●   Template name is returned from dispatch()
●   First templates/content subdirectory of
    module directory is searched
●   Then templates/content subdirectory of root
    directory is searched (allows for generic
              View templates
●   Currently written in PHP (not Smarty)
●   Should contain very limited logic – only
    display. No SQL, no messing about with form
    variables, just data display.
●   IMPORTANT: All URIs (URLs) that point
    back to the application must be generated by
    calls to $objEngine->uri()
             Content templates
●   Content template name returned by
    controller's dispatch() method
●   Contains page specific content – lists, forms,
    course content, discussion forum post etc
●   Not necessarily HTML can be XML, CSV or
    any other format
●   If necessary set layout template to null
                 Template variables
●   Data and/or objects are passed between controller and
    template via template variables
●   In controller:
      $this->setVar('rows', $rowsArray);
●   In view template:
      foreach ($rows as $row) {
           echo $objLanguage->languageText('Name').”: “;
           echo $row['name'];
●   [[[ sidenote about objLanguage: _('name') ]]]
       Example content template


<?php $objEngine->setVar('pageTitle',
                  $objLanguage->languageText(“Hello Title”) ?>

<h1><?php echo $objLanguage->languageText(“Hello Title”)</h1>

  <?php echo $objLanguage->languageText(“Hello”)?>,
  <?php echo $name ?>

<a href=”<?php echo $objEngine->uri('', 'main') ?>”>
  Home Page
              Layout templates
●   Contain common navigational elements
●   At present use $objSkin to provide
    skinnability – this is under discussion
●   Include content template's output via:
     <?php $objEngine->putPageContent() ?>

●   Have access to template variables set by
    content template
        Example layout templates



<title><?php echo $pageTitle ?></title>
<link rel=”stylesheet”
            href=”<?php echo $objSkin->stylesheet()?>” />
<body><?php putPageContent(); ?></body>
               Final words

Miscellaneous remaining topics and discussion /
Standard modules

●   language
●   security
●   config
●   context?
●   skin?
●   Extra layer of layout templates (one for
    module, one for application)?
●   Common baseclass for helper objects?
    Access to $objEngine?
●   Derek's tablesAdmin module – dynamic form-
    building and form-validation? XML
    description of data schema?
“There is certainly nothing keeping you from doing
MVC in ASP or PHP, even though that is not the
most immediately intuitive way to work with those
systems. In light frameworks like these, it is up to
you to introduce your own standards and systems to
organize your software.”
                                             -- Ian

To top