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

Developing a view-handling class - 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

Since the framework being developed here will implement a simple MVC layer, it needs to be able to parse view files in a basic way. Therefore, I'm going to define a class that performs that specific task by using an intuitive API.

This class is called, not surprisingly, “View,” and its partial definition is as follows:

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];

}

}

}

As shown above, the “View” class implements the complementary “__set()” and “__get()” PHP 5 magic functions to assign properties to a view and to retrieve their respective values dynamically. Additionally, the class accepts as an input argument the name of the file view that will be parsed, which is stored as an additional property.

The last thing that should be noticed is the implementation of a static factory method, which unlike the constructor, can be chained to others very easily.

So far, the set of tasks that this class executes is nothing special, right? But wait a minute! At this point, it truly lacks the most important feature -- it doesn’t have a method that allows it to parse properties created on the fly.

Well, naturally it’s necessary to code a method that tackles this process. But the full details will be covered in the last section of this tutorial, so click on the link below and keep reading.



 
 
>>> 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: