darwin_tech Posted April 7, 2011 Share Posted April 7, 2011 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"; Quote Link to comment https://forums.phpfreaks.com/topic/232987-reusing-a-method-multiple-times/ Share on other sites More sharing options...
KevinM1 Posted April 7, 2011 Share Posted April 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232987-reusing-a-method-multiple-times/#findComment-1198248 Share on other sites More sharing options...
phil88 Posted April 7, 2011 Share Posted April 7, 2011 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"); Quote Link to comment https://forums.phpfreaks.com/topic/232987-reusing-a-method-multiple-times/#findComment-1198253 Share on other sites More sharing options...
darwin_tech Posted April 7, 2011 Author Share Posted April 7, 2011 Hey, thanks for the replies Nghtslyr and Phil88. I hadn't thought about using an array and you are totally right. I have implemented a variation on Phil88's answer. Thankyou, Sam Quote Link to comment https://forums.phpfreaks.com/topic/232987-reusing-a-method-multiple-times/#findComment-1198274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.