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.
blog comments powered by Disqus |
|
|
|
|
|
|
|