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

Posted on 2005-04-12
Medium Priority
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
  • 2
  • 2
LVL 31

Author Comment

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

by:Marcus Bointon
ID: 13773994
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

Marcus Bointon earned 1500 total points
ID: 13774025
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

ID: 14329858
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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 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