Extract Variables Posted by: John in PHPionManager on
A few months ago, in one of the php articles / books I was reading I stumbled across something called variable variables. Essentially, it lets you create a variable whose name is defined by another variable. I thought to myself “where the hell would this be useful?” A few days after Jordan bloged about variable variables I actually found a use for them:

ionManager (a framework and CMS built myself), uses the MVC enterprise pattern. The model querys the database and generates/manipulates any necessary data; the view is responsible for displaying the data. Since the models are classes and views are procedural php files, having the model communicate its data with the view was a minor problem. My solution was to use the controller to pass any data generated by the model to the view.

For example I am creating a blog module (not do be confused with model) for ionManager. One of the views associated with the blog is a main view " this view simply shows a 300 word excerpt of the last ten blog entries. In the model, the code is rather elementary:
 
  1. function getList ()
  2.     {
  3.        
  4.         $result = $this->db->query("SELECT * FROM `jos_content` "
  5.                . "WHERE `created_by`='110' "
  6.                . "ORDER BY `created` DESC LIMIT 10");
  7.  
  8.         while ($blog = $this->db->fetch_assoc($result)) {
  9.             $time = strtotime($blog['created']);
  10.             $blog['created'] = date('F j, Y', $time);
  11.             $blog['fulltext'] = $this->cutText($blog['fulltext'], 300);
  12.             $blogs[] = $blog;
  13.         }
  14.         return $blogs;
  15.     }


The controller had a loadView function which accepted a single paramater " a path to the view file which it included. Moreover the controller can interact with the model directly since it is responsible for creating its instance. Therefore my controller code looked like this:
 
  1. public function index ()
  2.     {
  3.         $blogs = $this->model->getList();
  4.         $this->loadView("Blog/main_view.php");
  5.     }


Which is great, however main_view.php knows nothing about $blogs. I therefore chose to pass another parameter to the loadView function which I called $data. My idea was to pass in an assocative array whose index wold become the variable name, and the corrisponding value would become the variables value. Below shows the new controller code after these alterations:
 
  1. public function index ()
  2.     {
  3.         $blogs = $this->model->getList();
  4.         $data = array('blogs' => $blogs);
  5.         $this->loadView("Blog/main_view.php", $data);
  6.     }
Now main_view.php can use $blogs.


How does this work?
At first I used a fairly swift technique that took advantage of variable variables. For the sake of brevity, I removed several conditionals from the original function. The (now depreciated) function used the following logic:
 
  1. function loadView($path, $data)
  2. {
  3.     foreach($data as $foo => $bar) {
  4.         $$foo = $bar;
  5.     }
  6.    
  7.     include($path);
  8. }
$foo was the arrays index and $bar was the indexes value. I was fairly satisfied with this method until yesterday when I was reviewing the seventy two array functions and stumbled across extract. extract() produces the same exact result as my code " without a loop! Therefor my final code looks similar to:
 
  1. function loadView($path, $data)
  2. {
  3.     extract($data);
  4.     include($path);
  5. }


Now I am back to my original question " where the hell are variable variables useful?

Trackback(0)
feed0 Comments

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy