Jump to content

Recommended Posts

Hi, I am having some trouble re-using a method of a class which I use to setup my pages. I have an include which instantiates the class and then I make a call at the top of each page depending on the content required. I have several javascripts I want to include for a specific page and would like to just keep calling the same method to load them. If I try to call the method twice on the last one is echoed. I don't really want have to write out different methods for each file I may need.

 

Any help much appreciated.

 

 

 

The included class has this:

 

class Page {

   public function display_top() { 
     		
     # page specific atrributes		
     $this->display_js();

    }

    public function display_js() {
 echo "<script type='text/javascript' src='{$this->js}'></script>\n"; 
}
}

 

my page has this:

 

require ('classes/page_class.php');

$page = new Page;
$page->js 	= "js/jquery-1.3.2.min.js";
$page->js 	= "js/revealClass.js";

Link to comment
https://forums.phpfreaks.com/topic/232987-reusing-a-method-multiple-times/
Share on other sites

Instead of trying to call the method several times, why not simply pass in an array of scripts you want to have on each page?

 

class Page
{
   public function display_js($scripts)
   {
      foreach($scripts as $script)
      {
         echo "<script type='text/javascript' src='$script'></script>\n";
      }
   }
}

$scripts = array("js/jquery-1.3.2.min.js", "js/revealClass.js");

$page = new Page();
$page->display_js($scripts);

 

If you have one script that's necessary for every page, you can have it as a data member of your Page class, then add it as the first element of the incoming $scripts array.

The reason why your original code isn't doing what you want it to do is because this code;

 $page->js 	= "js/jquery-1.3.2.min.js";
$page->js 	= "js/revealClass.js";

Overwrites the $page->js property. It's the equivalent of doing;

$var = 'string 1';
$var = 'string 2';
$var = 'string 3';
echo $var;

Only the value that was last assigned to $var gets echo'ed.

 

 

As Nightslyr said, using arrays would be more appropriate. To keep with the original idea of using the js property of your Page class instead of passing the scripts as a parameter, you could do something like this;

 

class Page
{
   public function display_js()
   {
      foreach($this->js as $script)
      {
         echo "<script type='text/javascript' src='$script'></script>\n";
      }
   }
}

$page = new Page;
$page->js = array("js/jquery-1.3.2.min.js", "js/revealClass.js");

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.