Jump to content

Recommended Posts

Well in my script exist certain objects being used everywhere, in main script files and class library files. A good example is this Page class:

 

<?php
class Page{
  public $type;
  public $name;
  private $title = "";
  private $content = "";
  private $date = "";
  private $links;
  private $sidebar;
  private $ads;

  public function __construct($page = ""){
      // Constructor method of page class
  }

    public function gettitle(){
     if(empty($this->title)) throw new Exception('The page has no title.');
     return $this->title;
  }
  
  public function settitle($title){
     $title = secure($title);
     if(empty($title)) throw new Exception('Cannot set title for this page.');
 else $this->title = $title;
  }
  
  public function getcontent(){
     if(empty($this->title)) throw new Exception('The page has no content.');
     return $this->content;
  }
  
  public function addcontent($content, $overwrite = FALSE){
     $content = secure($content);
     if(empty($this->content) or $overwrite == TRUE) $this->content = $content;
 else $this->content .= $content;
  }

  public function getlinks(){
     // the method that grabs user links
  }

  public function getsidebar(){
     // the method that loads sidebar
  }

  public function getads(){
     // the method that shows ads
  }

  public function display(){
     // the method that format pages and brings everything together
  }
}
?>

 

Now lets say I have other classes such as User, Message and Item, a page object will need to be used inside some of their methods so that the page title/content can be modified when necessary. This seems to be quite problematic to me, since I have to declare this Page object global in every method that uses it.

 

Similar problem occurs with my database object, and its even more annoying since almost half of my class methods need to use database queries and commands.

 

So I was wondering... Is there a better way for me to handle these system objects such as Page and Database rather than having to declare them as global inside every single method that deals with them? Please help...

 

 

Another option is to look at one of the many "dependency injection" libraries around. Using these, your classes depend on a single "DI Manager" object, which in turn, stores all your dependencies.

 

Proem has one (there is a link in my signature) but there are also numerous stand alone options around.

Another option is to look at one of the many "dependency injection" libraries around. Using these, your classes depend on a single "DI Manager" object, which in turn, stores all your dependencies.

 

Proem has one (there is a link in my signature) but there are also numerous stand alone options around.

 

Sounds interesting, thanks for the advice. I am not quite sure if Dependency injection will work in this case as the page object is a system utility object whose properties/methods need to be accessible from both inside and outside classes. This is why I've been using global keyword for it, but I am sure there are better ways. Will study more into dependency injection to see if it indeed does the same thing as declaring global more efficiently.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.