Just in case you haven’t read the previous installment of the series, where I added to the framework the view-handling class mentioned in the introduction, below I listed the full source code corresponding to the file that contains this component. The file has been named “View.php” and 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 Aside from giving a concrete implementation to the “__set()” and “__get()” PHP 5 magic methods, which, when used in conjunction allow you to assign properties to view files dynamically, the workhorse method of the class is undoubtedly “display().” As seen above, this method is the one that actually parses a given view file by combining the functionality of the “extract()” PHP built-in function with output buffering. The logic implemented by this last method is so simple that it doesn’t bear any further discussion. Now that you’ve grasped how the previous “View” class does its thing, it’s time to add more core components to this example framework. Since the framework can now parse views through a separate class, the next logical step is to aggregate a component that allows it to handle the corresponding data layer, which for this project will be made up of one or more MySQL database tables. As I mentioned before, this brand new component will be a model class, and its definition will be shown in the following segment. Thus, to learn more about it, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|