HomePHP 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.
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){ $this->webPage=$webPage; } public function generateStyle(){ return str_replace('defaultstyle',$this->webPage- >getControllerStyle(),$this->webPage->getPage()); } }
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.