Proper Php 5 Classes and Objects Code Structure (with Smarty)

Posted on 2005-04-12
Last Modified: 2006-11-18
Im using the structure detailed in the book PHP 5 and MySQL E-Commerce From Novice to Professional.

Goal: To construct the text links on the home page.

The front page loads welcome.tpl which loads my custom function function.load_welcome.php and assigns it to a variable.

function.load_welcome.php creates the class Welcome.

class Welcome creates a new business object $this->mBoCorporate from class BoCorporate.

class BoCorporate contains

      public function CreateSiteLinks($name, $href, $class, $title)
            $link = "<a href=\"".$href."\"";
            if (isset($class))
                  $link .= " class=\"".$class."\"";
            if (isset($title))
                  $link .= " title=\"".$title."\"";
            $link .= ">".$name."</a>";
            return $link;

I use the function to assign values to an array via class Welcome in function.load_welcome.php this way

// Manages the home page
class Welcome
   /* public variables available in welcome.tpl */
   public $mWelcomeLink;
   /* private members */
   private $mBoCorporate;
   // constructor creates and business tier object instance
   function __construct()
      // creating the business tier object
      $this->mBoCorporate = new BoCorporate();
   // calls business tier method to read category list and create their links
   function init()
   // construct links
  $this->mWelcomeLink['board'] =
    $this->mBoCorporate->CreateSiteLinks('board members','mace.php?page=corporate_investor_relations');
  $this->mWelcomeLink['investor'] =
    $this->mBoCorporate->CreateSiteLinks('investor relations','mace.php?page=corporate_investor_relations');
  $this->mWelcomeLink['about'] =
    $this->mBoCorporate->CreateSiteLinks('about MACE','mace.php?page=corporate_about_us');
} //end class

I only showed three links to save space but there are actually a lot more.  I dont have a final number yet, but lets say there are 15-20 total.

Then I use {load_welcome assign=$welcome} in welcome.tpl and {$welcome->mWelcomeLink.about} to display the about us link, {$welcome->mWelcomeLink.investor} to display the link to investor relations, etc.

The other way I thought to do this, instead of using one function to create the links, is to create a separate function in the BoCorporate class for each link. Like this:

public function CreateLinkAboutUs
      return "<a href=\"mace.php?page=corporate_about_us\">About Us<//a>";

public function CreateLinkInvestorRelations
      return "<a href=\"mace.php?page=corporate_investor_relations\">Investor Relations<//a>";


then populate the array like this:

  $this->mWelcomeLink['about'] = $this->mBoCorporate->CreateLinkAboutUs();
  $this->mWelcomeLink['investor'] = $this->mBoCorporate->CreateLinkInvestorRelations();

My queston is:

                                     Is one way better than the other and, if it is, why?

Keep in mind that any page template throughout the site that grabs info from the database does so thru its own custom function.  So board_members.tpl calls {load_board_members assign="board"} which contains class BoardMembers, which calls a function in BoCorporate that calls a function in class DoCorporate (the data tier) with the end result of returning the names of the board members from the db query and populating variables used in the smarty template.

I can create all the site links by assigning these functions to a variable in the class created when each template loads.

One benefit would be when a link to a page changes, I wouldnt have to change it in every template that links to that page.  Whereas, using the CreateSiteLink() function, I would still have to change the values in every class that uses it to construct the same link.  With separate link functions, I only have to change the link once, in the BoCorporate class.

What I dont know is if there is a downside to adding 20 more functions to the BoCorporate class to do what could be done with one.
Question by:gwkg
    LVL 31

    Author Comment

    The page I am working on is located at  The links I am talking about are not the navigation links on the left.  (Although, they could/may/will be created thru the object created by class Menu when menu.tpl loads.)  

    The links I am making now are going to be scattered throughout the grey descriptions of the site sections.  They may or may not be finished, depending on how long ago I wrote this.

    LVL 25

    Expert Comment

    I just wouldn't do it this way. I avoid having any kind of HTML output anywhere in my objects - otherwise you effectively have bits of template in your objects, which entirely undermines the point of using Smarty. I pass my objects to the template, and the template renders them to HTML. If you need to render links in a special way, I'd suggest you write a Smarty extension.

    I think doing this:

    public function CreateLinkAboutUs
          return "<a href=\"mace.php?page=corporate_about_us\">About Us<//a>";

    is a very bad idea. Not only do you then have template in code, but you also have content in code. Even having the query string in code is too much - it should be constructed dynamically within the template having been passed the page target.

    Pass your welcome object to the template:

    $smarty->assign('welcome', $welcome);

    Then output stuff as required in the template:

    {foreach $welcome->mWelcomelink item=link_url key=link_name}<a href="mace.php?page={$link_url}">{$link_name}</a><br />{/foreach}
    LVL 25

    Accepted Solution

    Another important reason for doing it this way - you might want to render the same set of links in a completely different way (e.g. in a side panel or as a graphical menu bar) - with your fixed code you just can't do that - unless you start having functions for every possible scenario, at which point your code base will disintegrate...
    LVL 31

    Author Comment

    I noticed this question still open and cant believe how far Ive come since asking it.  You were right, of course.  That was not good code.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now