Jump to content

[SOLVED] Can't access array in a class


percent20

Recommended Posts

I have been trying to access a public array inside a class, but it is freaking out on me.  Should this be able to work?

 

$TestArra = array('Hello', ' ', 'World');

class Test
{
function Test()
{
	$this->Display();
}

function Display()
{
	foreach($TestArray as $value)
	{
		echo $value;
	}
}
}		

 

shouldn't that display "Hello World" on the page? instead i get this error:

 

Warning: Invalid argument supplied for foreach() in C:\xampplite\htdocs\xampp\Links\test.php on line

 

Maybe I just don't understand something.  Any help would be great.

 

Thanks.

 

p.s. Also I can't create an array for some reason either. I tried $a = array('a'); and $a[0] = 'a'; neither would seem to work in the class.

Link to comment
Share on other sites

To stop dancing around the point, your array example fails for one of two possible reasons, depending on what you're trying to do:

 

1. If your array is supposed to be part of the class, it fails because the array is not declared as a property of the class.

2. If your array is not supposed to be part of the class, it fails because you're not passing it to the object as a method argument.

 

Example of using the array as a property of the class:

<?php
   class Test
   {
      private $testArray = array('Hello', ' ', 'World'); //best to keep it private, as you shouldn't give the outside world direct access to class properties

      public function __construct()
      {
         $this->display();
      }

      public function display()
      {
         foreach($this->testArray as $value)
         {
            echo $value;
         }
      }
   }

   $myObj = new Test();
?>

 

Example of passing the array as an argument:

<?php
   $testArray = array('Hello', ' ', 'World');

   class Test
   {
      public function __construct($array)
      {
         $this->display($array);
      }

      public function display($array)
      {
         foreach($array as $value)
         {
            echo $value;
         }
      }
   }

   $myObj = new Test($testArray);
?>

 

Note: the examples I've written use PHP 5 syntax.

Link to comment
Share on other sites

Finally figured out how to get it working.  Since it needs to be independent and global I had to use the global keyword inside the class to pull it into scope.  I didn't know about the global until yesterday. Until I can convince others here to do non-public global variables for config it has to be done this way :(

 

Thanks for the help.

Link to comment
Share on other sites

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.