Home arrow PHP arrow Page 4 - Building a Web Page Controller for Simulating the Model-View-Controller Schema in PHP

Completing the MVC schema: defining a style sheet generator class - PHP

If you’re one of those PHP developers that want to extend your background in object-based applications, then this series might be quite attractive to you. In these three tutorials (of which this is the second), you’ll learn how to simulate a simple Model-View-Controller schema, which can be easily expanded to construct more complex relationships between the different entities, in this case applicable specifically to PHP classes.

  1. Building a Web Page Controller for Simulating the Model-View-Controller Schema in PHP
  2. Creating the first component of the schema: defining a web page controller class
  3. Creating a real-world model: defining a web page generator class
  4. Completing the MVC schema: defining a style sheet generator class
  5. Putting the MVC schema to work: generating style sheets on the fly
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
August 14, 2006

print this article



As I explained in the previous section, the “View” element included within the MVC schema will be represented by a style sheet generator class. This class will be tasked with rendering the previous web page in accordance with the type of style indicated originally by the corresponding web page controller.

Based on the above concepts, this new class is defined as follows:

// define 'StyleGenerator' class (view)
class StyleGenerator{
    private $webPage;
    public function __construct(WebPage $webPage){
    public function generateStyle(){
        return str_replace('defaultstyle',$this->webPage-

As you can see, the "StyleGenerator" class listed above also accepts the respective "WebPage" object (remember again that this is the model), and uses the "generateStyle()" method to attach a style sheet to the whole web page. Again, you should notice how the pertinent style is tied to the web document, based on the "decisions" made by the controller class.

At this stage, I feel pretty satisfied, because I’ve defined the three elements that comprise the complete MVC schema. Therefore, it’s time to put all the classes together and see how different style sheets (called "views") can be dynamically attached to the web document created by the previous "WebPage" class.

To see how this will be done, you’ll have to read the upcoming section, thus click on the following link.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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