Jump to content

Recommended Posts

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

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 />";
}

?>

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

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.

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.

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.