Solved

want a understanding any of the "why" questions that contributed to the framework design

Posted on 2011-09-07
7
211 Views
Last Modified: 2012-05-12
ideally in a video tutorial

I would like to learn the theory behind creating a framework

I am learning about mvc framework.
0
Comment
Question by:rgb192
7 Comments
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 125 total points
ID: 36494527
I don't know if this is of interest to you, but there is a PHP Theory and Design Forum at

http://www.devnetwork.net/viewforum.php?f=19

where lots of interesting questions and solutions are proposed.
0
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 125 total points
ID: 36494538
Take a look at this article,   Why PHP Frameworks Matter
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 125 total points
ID: 36494638
The point of a framework is to provide consistency and reduce workload.

For example, we have a custom framework for small websites. It is a working website with very basic styling, the maintenance system already built in, the basic tables in the database, folders for website images, folders for uploaded images and so forth.

When a customer comes to us we can take that framework and get everything running very quickly because most of what we need is in place.

All frameworks aim to achieve this. They point is to allow code to be employed by working in a standard way so that tools can be created to speed up development, deployment and maintenance. You may even have multiple frameworks - another of ours is used to write custom code classes. We supply the data definitions as XML and out pops PHP code. The layout of the generated code is always the same so we are used to modifying these classes very quickly.

So as an example let us construct a table to hold data in a database and a class to access that table. We start by defining the table in XML because this is how this framework works

    <dbTable name='rgb192' desc='An example for RGB192' prefix='rgb' >
          <dbField name='id'           type='int'    size=''    desc='Auto id'   primary='Yes' auto='Auto' />
          <dbField name='firstName'    type='string' size='32'  desc='The persons first name' />
          <dbField name='lasttName'    type='string' size='32'  desc='The surname' />
          <dbField name='dob'          type='date'   size=''    desc='Date of birth' />
          <dbField name='priority'     type='int'    size=''    desc='Priority code 1=urgent, 2=normal, 3=none' />                    
     </dbTable>     

Open in new window



The code generator then produces the following code classes. I will not pretend that this is 'excellent' code or 'efficient' code, it has evolved over years and originally came from Java, but it is robust and works well.

First, the base class that manages the basic operations on the table

<?php
/**
 * An example for RGB192
 *
 *
 *
 * {@internal
 * Assumptions
 * -----------
 *
 *
 * Dependencies on other classes
 * -----------------------------
 *
 *
 *
 * Modification list
 * -----------------
 * Mod.   By    When        Comment
 * ---    --    ------      -------
 *
 *
 * }}
 */


class rgb192 extends BaseDataClass {

     /**
      * @var int Auto id
      */
      protected $rgbId;


     /**
      * @var string The persons first name
      */
      protected $rgbFirstName;


     /**
      * @var string The surname
      */
      protected $rgbLasttName;


     /**
      * @var string (Actually defined as DATE yyyy-mm-dd) Date of birth
      */
      protected $rgbDob;


     /**
      * @var int Priority code 1=urgent, 2=normal, 3=none
      */
      protected $rgbPriority;





     /**
      * The name of the table
      *
      * @private
      */
     protected $tableName;



     // ----- Constructor -----------------------------------------------------

     function __construct( $id=-1, $tableName="rgb192") {


          parent::__construct();   // parent class constructor

          $this->initialise( $id );

          $this->tableName  = $tableName;
          $this->createTable();


          if ( $this->exists() )
               $this->rtv();
     }


     // ----- Setter and getter methods ---------------------------------------

     /**
      * Set auto id
      */
     function setRgbId( $prmRgbId ) {
          $this->rgbId = $prmRgbId;
     }

     /**
      * Get auto id
      */
     function getRgbId() {
          return $this->rgbId;
     }

     /**
      * Set the persons first name
      */
     function setRgbFirstName( $prmRgbFirstName ) {
          $this->rgbFirstName = $prmRgbFirstName;
     }

     /**
      * Get the persons first name
      */
     function getRgbFirstName() {
          return $this->rgbFirstName;
     }

     /**
      * Set the surname
      */
     function setRgbLasttName( $prmRgbLasttName ) {
          $this->rgbLasttName = $prmRgbLasttName;
     }

     /**
      * Get the surname
      */
     function getRgbLasttName() {
          return $this->rgbLasttName;
     }

     /**
      * Set date of birth
      */
     function setRgbDob( $prmRgbDob ) {
          $this->rgbDob = $prmRgbDob;
     }

     /**
      * Get date of birth
      */
     function getRgbDob() {
          return $this->rgbDob;
     }

     /**
      * Set priority code 1=urgent, 2=normal, 3=none
      */
     function setRgbPriority( $prmRgbPriority ) {
          $this->rgbPriority = $prmRgbPriority;
     }

     /**
      * Get priority code 1=urgent, 2=normal, 3=none
      */
     function getRgbPriority() {
          return $this->rgbPriority;
     }




     // ----- Private/Protected methods ---------------------------------------

     /**
      * Create a table for the class. It doesn't matter
      * if this fails so just run it anyway. It is more
      * important to ensure that the table exists.
      */
     protected function createTable() {
          $qry = "CREATE TABLE IF NOT EXISTS $this->tableName (
                       rgbId INT NOT NULL  auto_increment ,
                       rgbFirstName VARCHAR(32) NOT NULL,
                       rgbLasttName VARCHAR(32) NOT NULL,
                       rgbDob DATE NOT NULL default '0000-00-00',
                       rgbPriority INT NOT NULL default 0,
                       ts timestamp NOT NULL default NOW(),
                       PRIMARY KEY( rgbId )
          )";


          $this->actionQuery($qry,__FILE__,__LINE__);
     }



     /**
      * Initialise fields
      */
     protected function initialise( $id=-1 ) {
          $this->rgbId           = $id;
          $this->rgbFirstName    = "";
          $this->rgbLasttName    = "";
          $this->rgbDob          = '0000-00-00';
          $this->rgbPriority     = 0;

     }


     /**
      * Check if a record with this key exists in the table managed by this class
      */
     protected function exists() {
          $qry = "select rgbId from $this->tableName where rgbId='".intval($this->rgbId)."'";

          $this->selectQuery($qry,__FILE__,__LINE__);
          return ( mysql_num_rows($this->rs) > 0 );
     }









     // ----- Standard class methods ------------------------------------------

     /**
      * Add the data stored in the class to the table managed by the class.
      */
     function add() {
          if ( !$this->exists() ) {
               $qry = "insert into $this->tableName
                           (
                              rgbFirstName,
                              rgbLasttName,
                              rgbDob,
                              rgbPriority
                           ) values ( 
                                '".$this->text($this->rgbFirstName)."',
                                '".$this->text($this->rgbLasttName)."',
                                '".$this->text($this->rgbDob)."',
                                '".$this->stripInt($this->rgbPriority)."'
                           ) ";

               $this->actionQuery( $qry, __FILE__, __LINE__ );
          }
     }


     /**
      * Retrieve the data stored in the class to the table managed by the class.
      */
     function rtv() {
          $qry = "select * from $this->tableName where rgbId='".intval($this->rgbId)."'";
          $this->selectQuery( $qry, __FILE__, __LINE__ );

          if ( $this->returnedSomething() ) {
               $rw = $this->assoc(); 
               $this->rgbId           = $rw['rgbId'];
               $this->rgbFirstName    = $rw['rgbFirstName'];
               $this->rgbLasttName    = $rw['rgbLasttName'];
               $this->rgbDob          = $rw['rgbDob'];
               $this->rgbPriority     = $rw['rgbPriority'];
          }
          else
               $this->initialise();

     }


     /**
      * Retrieve the data by an ID code
      */
     function rtvById( $id ) {
          $this->initialise();
          $this->rgbId = $this->text($id);
          $this->rtv();
     }




     /**
      * Update the data stored in the class to the table managed by the class.
      */
     function update() {
          if ( $this->exists() ) {
               $qry = "update $this->tableName set
                              rgbId         = '".$this->stripInt($this->rgbId)."',
                              rgbFirstName  = '".$this->text($this->rgbFirstName)."',
                              rgbLasttName  = '".$this->text($this->rgbLasttName)."',
                              rgbDob        = '".$this->text($this->rgbDob)."',
                              rgbPriority   = '".$this->stripInt($this->rgbPriority)."'
                              where rgbId='".intval($this->rgbId)."'";
               $this->actionQuery( $qry, __FILE__, __LINE__ );
          }
     }



     /**
      * Change or create a record
      */
     function chgCrt() {
          if ( $this->exists() )
               $this->update();
          else
               $this->add();
      }



     /**
      * Delete the data stored in the class from the table managed by the class.
      */
     function delete() {
          $qry = "delete from $this->tableName where rgbId='".intval($this->rgbId)."'";

          $this->actionQuery( $qry, __FILE__, __LINE__ );
     }


     // ----- Public methods --------------------------------------------------




} // End of class rgb192

Open in new window



Next, an extension to the base class. In this one we put all extra code that encodes business rules. That way if we need to add an extra field to the base class we change the XML, regenerate the code and only replace the base class in the live website so that all existing business rules in the extended class are unaffected.

<?php
/*
 * comment
 * -------
 * 
 *
 *
 * Assumptions
 * -----------
 *
 *
 * Dependencies on other classes
 * -----------------------------
 * rgb192
 *
 *
 * Modification list
 * -----------------
 * Mod.   By    When        Comment
 * ---    --    ------      -------
 *
 */


class usrRgb192 extends rgb192 {

     /*
      * List of properties for this class
      *
      * rgbId          int          Auto id
      * rgbFirstName   string       The persons first name
      * rgbLasttName   string       The surname
      * rgbDob         string       Date of birth
      * rgbPriority    int          Priority code 1=urgent, 2=normal, 3=none
      *
      */


     // ----- Constructor -----------------------------------------------------

     function __construct( $id=-1, $tableName="rgb192") {

          parent::__construct( $id, $tableName );   // parent class constructor
     }




     /**
      * Delete the data stored in the class from the table managed by the class.
      */
     function getDeleteString() {
          return "rgb192";
     }



} // End of class Usrrgb192

Open in new window



If we wished to add a function that allowed us to retrieve all data in descending date of birth order then we would alter the extension class to be like so

<?php
/*
 * comment
 * -------
 * 
 *
 *
 * Assumptions
 * -----------
 *
 *
 * Dependencies on other classes
 * -----------------------------
 * rgb192
 *
 *
 * Modification list
 * -----------------
 * Mod.   By    When        Comment
 * ---    --    ------      -------
 *
 */


class usrRgb192 extends rgb192 {

     /*
      * List of properties for this class
      *
      * rgbId          int          Auto id
      * rgbFirstName   string       The persons first name
      * rgbLasttName   string       The surname
      * rgbDob         string       Date of birth
      * rgbPriority    int          Priority code 1=urgent, 2=normal, 3=none
      *
      */


     // ----- Constructor -----------------------------------------------------

     function __construct( $id=-1, $tableName="rgb192") {

          parent::__construct( $id, $tableName );   // parent class constructor
     }



     /**
      * Fetch an array of objects by descending Date of Birth
      * @return An array of objects
      */
     function fetchByDescendingDoB() {
          $arr = array();

          $qry = "select * from $this->tableName order by rgbDob desc";
          $this->selectQuery( $qry, __FILE__, __LINE__ );

          if ( $this->returnedSomething() )
               while ($rw = $this->assoc() )
                    $arr [] = new usrRgb192( $rw['rgbId'] );
               
          return $arr;
     }

     

     /**
      * Delete the data stored in the class from the table managed by the class.
      */
     function getDeleteString() {
          return "rgb192";
     }



} // End of class Usrrgb192

Open in new window



This way, changes in the base class do not affect our existing code and we can use this code in a website for further processing.

This framework also produces what we call 'panel' code for displaying a form with HTML, but this reply is long enough already. The point is that starting with a simple peice of XML and using the framework, we can produce a LOT of working code very, very quickly and because it is in a standard format we know exactly how to modify it, handle it and update it consistently and safely.

You do not have to use Zend or Yii or any other framework. You can roll your own to match your way of working. In MVC parlance the stuff above is the "MODEL" and the panel code I referred to manages HTML and the model code so it would be the CONTROLLER. The VIEW is largely controlled via CSS.


0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 36496111
I think the best part of bportlock's answer is this: because it is in a standard format we know exactly how to modify it -- that is the "why" answer.  The rest of this iceberg is the part about "we know" the framework.  If you are experienced in object-oriented PHP you can get to know a framework with relative ease.  Note I said "ease" and not "speed" because there is a learning curve for frameworks that is similar to the learning curve for programming languages.  You have to use it enough to become fluent before you can really be productive (perhaps six months).

You can find a lot of good general information about PHP frameworks in the online public sources.
http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks#PHP
http://davidjconnelly.wordpress.com/2011/07/03/the-best-php-framework-of-2011/
http://www.php-developer.org/most-used-php-framework-the-popular-top-7-list-in-year-2011/

Each framework will have its own introductory materials. Example.
http://framework.zend.com/manual/en/learning.quickstart.intro.html

If you really want video, you might consider searching YouTube.  I have never done that because I prefer books to online materials, but I bet there are video lectures on just about everything, including PHP frameworks.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 36496407
Ray said: "If you really want video, you might consider searching YouTube..."

Spot on Ray.... I have no idea if they are any good, but they are certainly out there

Symfony - http://www.youtube.com/watch?v=hlAdE8xPLj0
Zend - http://www.youtube.com/watch?v=5d675aSOssg&feature=related
Cake - http://www.youtube.com/watch?v=NnvOY8y_Lr0
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36496670
Brian: A friend of mine is in the business of privatizing USA charter schools.  His firm, Mosaica, outsources all of the non-pedagogical activities, leaving only the "teaching to the teachers," so to speak.  We had lunch a few months ago when he was in DC, and we discussed the use of video lectures.  It's clear to me that the wide availability of online video will turn education on its head.  The "homework" will be to watch the video lecture (formerly presented in the classroom setting) and the "classwork" will be to work collaboratively to solve the problems or craft the descriptive paragraphs (formerly done in solitary at home).  So the teacher will go from the role of talking head imparting wisdom, to the interpreter of thoughts and the facilitator of designs.  I cannot imagine a greater revolution in education!

Over and out, best to all, ~Ray  
0
 

Author Closing Comment

by:rgb192
ID: 36518557
thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now