Archive
Writing dynamic classes with PHP
I recently had the necessity to write a dynamic class (i.e. where methods, or properties of the class are determined during runtime) and in doing some research, stumbled on to this very helpful article :
The author (Jack Herrington) goes to a lot of trouble explaining how the magic __get(), __set() and __call() methods work and how best to use them.
It does get a little bit complicated towards the end, but is a good example of a)why one should or need to use a dynamic class/object and b)how to implement it successfully within your application.
PHP wrapper class for Zend View
I recently posted an article with an example of a Zend Session wrapper I’ve been using for some of my projects.
In that same vein I’ve created a simple wrapper for Zend View. I found this useful in instances where I didn’t need to use Zend Framework’s whole MVC implementation. Zend View is extremely helpful in creating a quick templating system for smaller projects or custom frameworks.
Once again I’ll be assuming the following:
- You have installed Zend Framework and set up the necessary include paths
- You have read the documentation pertaining to Zend_View
First our view wrapper/abstraction class (view.php) :
<?php
/**
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to calisza@gmail.com so we can send you a copy immediately.
*
* @package FlexiDev_View
* @copyright Copyright (c) 2007 - 2008, Flexible Developments - Barry Roodt (http://calisza.wordpress.com)
* @license New BSD License
*/
class View {
public $tmplVars;
public $view;
/**
Constructor
*/
public function __construct(){
$this->tmplVars = array();
// Instantiate our Zend View
$this->view = new Zend_View();
/**
* reset our basepath and scriptpaths, this is because we don't need the 'scripts', 'helpers' and 'filters' sub folders
* relative to this script - of course you could change this as your needs require
*/
$this->view->setBasePath("./view");
$this->view->setScriptPath("./view");
// set and assign some global vars to our template
// this is just an example - you can add more here
$globals = array("base_url" => $_SERVER["HTTP_HOST"]);
$this->view->assign($globals);
}
/**
* Method to add to our template variable stack
* @param string variable name
* @param mixed value
*/
public function addTmplVar($key, $value){
/* If our stack doesn't already exist, create a new array */
if (!is_array($this->tmplVars)){
$this->tmplVars = array($key=>$value);
} else {
// Add to our variable stack
$this->tmplVars[$key] = $value;
}
}
/**
* Method to render the required view/template
* @param string template script name
* @param bool (optional) clear variable stack (default=true)
*/
public function getTemplate($tmpl, $clearVars=true){
/* Add our variable stack to the template */
$this->view->assign($this->tmplVars);
/* If required, clear our stack so that we can start with a fresh template on the next call */
if ($clearVars)
$this->tmplVars = "";
/* Return the rendered template */
return $this->view->render($tmpl);
}
}
?>
Next for some example usage (index.php) :
<?php
require_once("View.php");
$items = array ("Name" => "Barry", "Surname" => "Roodt", "Title" => "Mr");
$view = new View();
$view->addTmplVar("myitems", $items);
echo $view->getTemplate("template.phtml");
?>
And finally our view template (template.phtml) :
<div>Hi <?php echo $this->myitems["name"]; ?></div>
<div>Here is some information for you
<?php foreach($this->myitems as $key=>$val) : ?>
<p><strong><?php echo $key; ?>: </strong><?php echo $val; ?></p>
<?php endforeach; ?>
</div>
<div>You can also use Zend View's default helpers like so : <?php echo $this->escape($this->myitems["name"]); ?></div>
You can also view an example of this class in action here.
As always, comments, critiques and corrections welcome.
