Home arrow PHP arrow Page 2 - Creating a Model Class for an MVC-based Framework

Review: the View class - PHP

In this ninth part of the series, I add an extensible model class to the framework. This class can be used for running CRUD operations against a selected MySQL database table.

TABLE OF CONTENTS:
  1. Creating a Model Class for an MVC-based Framework
  2. Review: the View class
  3. Managing the database layer with a basic model class
  4. Deleting database records with a final model class method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
June 28, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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