combitz Posted June 25, 2009 Share Posted June 25, 2009 I am looping through an array of objects, but for the example below I've just made it a simple array for clarity. Each item may or may not be the same value but I want each item to call the same function name but with a different response to the item that gets built/output. What I've done is created a different file to include (or require) based on the type of item but I keep getting errors with the second iteration, can anyone point me in the right direction. my simple loop $animals = Array('dog', 'cat', 'bird'); foreach($animals as $a){ require_once("./includes/".$a.".php"); echo"animal = ".$a." says ".speak()."<br />"; } an example of the dog.php include file function speak(){ return "woof"; } I want to call the speak function relative to the type of animal I get the error: animal = dog says woof Fatal error: Cannot redeclare speak() (previously declared in C:\wamp\www\tests\includes\dog.php:7) in C:\wamp\www\tests\includes\cat.php on line 8 any help is appreciated thanks Link to comment https://forums.phpfreaks.com/topic/163619-using-require_once-in-a-loop/ Share on other sites More sharing options...
Jibberish Posted June 25, 2009 Share Posted June 25, 2009 Its because the functions called the same thing in all the files I imagen. You could just use a case statement <?php function speak($animal) { switch($animal) { case 'dog': echo 'woof'; break; case 'cat': echo 'meow'; break //etc } } $animals = Array('dog', 'cat', 'bird'); foreach($animals as $a){ echo"animal = ".$a." says ".speak($a)."<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/163619-using-require_once-in-a-loop/#findComment-863294 Share on other sites More sharing options...
combitz Posted June 25, 2009 Author Share Posted June 25, 2009 I can't do that as it is working with someone elses code and I just need the results from the items they build. The problem is I can't call their functions without the correct require_once files. If it helps anyone in understanding what I'm trying to do, I'm trying to develop a scorm plugin for Moodle at moodle.org and they use different include files based on a value in the object type, (E.G. $object->[version_no]->1_3 would include the file mon/scorm/include/lib1_3.php which includes the methods to create and return the data I need) I don't think its a Moodle issue but more a coding style or lack of my code skills cheers Link to comment https://forums.phpfreaks.com/topic/163619-using-require_once-in-a-loop/#findComment-863298 Share on other sites More sharing options...
dzelenika Posted June 25, 2009 Share Posted June 25, 2009 I suppose that every include file have your own fuction speak(). You cannot have multiple times include function with same name. Anyhow I think your access to this problem solving is wrong. Link to comment https://forums.phpfreaks.com/topic/163619-using-require_once-in-a-loop/#findComment-863299 Share on other sites More sharing options...
combitz Posted June 25, 2009 Author Share Posted June 25, 2009 Yes, each include file has a function speak() The current code uses this method in Moodle and it works well, until I try to make my own function to loop through my items. it reminds me of polymorphism but I don't understand how php 5 deals with it. Link to comment https://forums.phpfreaks.com/topic/163619-using-require_once-in-a-loop/#findComment-863302 Share on other sites More sharing options...
Daniel0 Posted June 25, 2009 Share Posted June 25, 2009 The thing is that: test1.php <?php echo 'foo'; test2.php <?php require 'test1.php'; echo 'bar'; Is the same as doing: <?php echo 'foo'; echo 'bar'; So you can't do like <?php function speak() { /* do something */ } function speak() { /* do something else */ } speak(); This is exactly why putting things in the global namespace is a bad idea, and why the namespaces support in PHP 5.3 (due to be released next Tuesday) is much awaited. Link to comment https://forums.phpfreaks.com/topic/163619-using-require_once-in-a-loop/#findComment-863320 Share on other sites More sharing options...
Skepsis Posted June 25, 2009 Share Posted June 25, 2009 You would have to use classes. Each file you include, make it a class instead of just a function, you could do: // file - dog.php class dog { var $name = 'dog'; function speak() { return $this->name.' Says Woof'; } } // file - bird.php class bird { var $name = 'bird'; function speak() { return $this->name.' Says Chirp'; } } // then you would have: $animals = Array('dog', 'cat', 'bird'); foreach($animals as $a) { if(file_exists(includes/($a}.php")) { require_once("includes/".$a.".php"); $doityourself = new $a; echo "animal ".$a." says ".$doityourself->speak()."<br />"; } } I think this would solve your problem. Link to comment https://forums.phpfreaks.com/topic/163619-using-require_once-in-a-loop/#findComment-863627 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.