Home arrow PHP arrow Page 4 - Dealing with Views in an MVC-based Framework in PHP 5

Adding a method for parsing data - PHP

In this eighth part of the series, I add to our example MVC-based framework another crucial component. In this case, it's a class that parses data usually injected from a controller and rendered in the form of HTML pages.

TABLE OF CONTENTS:
  1. Dealing with Views in an MVC-based Framework in PHP 5
  2. Review: adding caching capabilities to the sample framework
  3. Developing a view-handling class
  4. Adding a method for parsing data
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
June 23, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the course of the preceding section, I pointed out that we need to add a method to the “View” class that parses dynamically-assigned properties within the corresponding view file. To fit this requirement, below I listed the complete source code of the file that contains the view handling class, this time including the aforementioned data-parsing method, called “display().”

The additional file looks like this:

(View.php)

<?php

class View

{

private $viewfile = 'default_view.php';

private $properties = array();

// factory method (chainable)

public static function factory($viewfile = '')

{

return new self($viewfile);

}

// constructor

public function __construct($viewfile = '')

{

if ($viewfile !== '')

{

$viewfile = $viewfile . '.php';

if (file_exists($viewfile))

{

$this->viewfile = $viewfile;

}

}

}

 

// set undeclared model property

public function __set($property, $value)

{

if (!isset($this->$property))

{

$this->properties[$property] = $value;

}

}

// get undeclared model property

public function __get($property)

{

if (isset($this->properties[$property]))

{

return $this->properties[$property];

}

}

// populate view with data and return contents

public function display()

{

extract($this->properties);

ob_start();

include($this->viewfile);

return ob_get_clean();

}

}// End View class

The method, tasked with dynamically parsing properties of the view, combines the functionality of the “extract()” PHP built-in function with output buffering. In this particular case, once the properties assigned to the view object have been property extracted, they’re embedded into the corresponding view file.

This means that this file must be coded as a simple HTML page containing interspersed PHP code, which is an efficient approach, since PHP is itself a great templating language.

Of course, you’ll understand more clearly how view files will be parsed via the “View” class shown before when I code an example that puts the framework into action. 

Final thoughts

In this eighth installment of the series, I added to this example MVC-based framework another crucial component: a class whose main task was parsing data usually injected from a controller and rendered in the form of HTML pages.

The addition of this brand new class now lets the framework handle different views in a straightforward way, thus constructing the View layer that comprises the MVC pattern.

In the next tutorial, I’m going to add another fundamental component to the framework that will responsible for handling the database layer. I’m talking about building a model class, and its progressive development will be covered in upcoming part of the series. Don’t miss it!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: