Page 1 of 1

Procedural or OO?

Posted: Tue Jun 17, 2008 5:31 am
by dbemowsk
I am looking for peoples opinions on what is better in this situation. I use a template parser to generate my output for my framework. I use a procedural approach to it that is as follows:

Say I have this simple template.

Code: Select all

 
<html>
<head>
    <title>{title}</title>
</head>
<body>
    {body}
</body>
</html> 
 
This is the bit of code that I would use to "fill" the template

Code: Select all

 
<?php
//Include the template parser class file into our script
include "templateParser.php";
//Define our tags
$tags['title'] = "Hello";
$tags['body'] = "Hello World";
//output the page
$output = templateParser::parseTemplate($tags, "mytemplate.tpl");
echo $output;
?> 
 
The template parser class just opens the template file as defined in the second parameter "mytemplate.tpl", and performs a basic str_replace using the $tags array where the keys of that array are the tag names defined within the template and the values of the array are what gets inserted.

My main question is... is this approach, being procedural, any better or worse than going with a template parser that is object oriented in nature where you would use it something like this?

Code: Select all

 
<?php
//Include the template parser class file into our script
include "templateParser.php";
$template = new templateparser();
//Define our tags
$template->set_tag("title", "Hello");
$template->set_tag("body", "Hello World");
//output the page
$output = $template->render();
echo $output;
?> 
 
And why?

Re: Procedural or OO?

Posted: Tue Jun 17, 2008 5:42 am
by onion2k
In the example you give there's no difference so it doesn't really matter which you opt for.

In the future though... then you'll find the object oriented approach a bit easier to extend and build upon, especially if you start moving beyond straightforward tag replacements. Consequently object oriented is a better way to go now.

Re: Procedural or OO?

Posted: Tue Jun 17, 2008 11:52 am
by Christopher
The main distinction between OO and procedural is that how they represent data. The above example is very simple, but if you want to modularize your application, which is a standard way to deal with complexity, then you would need to pass the template data around. With the procedural version you need to pass $tags, "mytemplate.tpl", and any other settings vars you might need. The more complex the more vars need to be passed. With the OO version you only ever need to pass around $template.

OO is really not that sophisticated a concept at it most basic level -- you can define data structures and use a simple syntax to invoke functions associated with them. There are obviously a bunch of other advantages that grow out of that simple concept, such as inheritance, polymorphism, etc. Then there are a bunch of interesting associated benefits like testing methodologies, patterns, etc. that have grown from the same concept.

That's why comparing simple examples like this usually misses the point. It is like people a century ago arguing about whether combustion or electricity is better. For basic motors and lights there probably were good arguments for both at the time ... no one foresaw all the other things that would grow out of the availability of electricity.

Re: Procedural or OO?

Posted: Fri Jun 20, 2008 9:43 pm
by dbemowsk
Thanks for the responses.

Re: Procedural or OO?

Posted: Sat Jun 21, 2008 7:54 am
by dbemowsk
So I am working on switching my template parsing logic. After doing some more google searches on this topic, I found that people were saying that it was a good idea to separate business logic from display or presentation logic. It took me a little bit to re-figure that theory in my head, but I think I am getting it now. The business logic should only be concerned with retrieving the needed data for what you are trying to do. The display logic (i.e. the template parser and templates) should do all the work of displaying that data.

I was previously under the assumption that having PHP code in your template files was a bad idea. After looking at some simple lines of code that use output buffering, I am seeing that having PHP code in the templates can be very useful and EASY to deal with. Not to mention it should REALLY speed up my code. Here is the simple rendering method that I added to my templateparser:

Code: Select all

 
    function render($template = NULL) {
        $template = (is_null($template)) ? $this->_path.$this->_tpl_file : $this->_path.$template;
        extract($this->_tags);
        ob_start();
        include($template);
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    } //End function render
 
Changing my template parser to use an object oriented approach was a lot easier than I thought. The hardest part now is going to be converting all my existing modules over to using this new approach, but I think it is going to speed up the page loading times immensely since the class files for some of the modules will not be so bulky.

I may need to add a few more things to the template parser, but for the most part, I have the basic concept working.

Thanks again for your input guys.