Can't access array in a class

Reply

Join Date: Jan 2005
Posts: 33
Reputation: percent20 is an unknown quantity at this point 
Solved Threads: 0
percent20 percent20 is offline Offline
Light Poster

Can't access array in a class

 
0
  #1
Jun 23rd, 2008
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?

  1. $TestArra = array('Hello', ' ', 'World');
  2.  
  3. class Test
  4. {
  5. function Test()
  6. {
  7. $this->Display();
  8. }
  9.  
  10. function Display()
  11. {
  12. foreach($TestArray as $value)
  13. {
  14. echo $value;
  15. }
  16. }
  17. }

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.
Last edited by percent20; Jun 23rd, 2008 at 6:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Can't access array in a class

 
0
  #2
Jun 23rd, 2008
  1. <?
  2. $objtest = new Test(array('Hello', ' ', 'World'));
  3. $objtest->echoArrayA1();
  4. $objtest->echoArrayA2();
  5.  
  6. class Test
  7. {
  8. var $TestArray = array();
  9.  
  10. function Test($inputArray)
  11. {
  12. $this->setTestArray($inputArray);
  13. $this->Display();
  14. }
  15.  
  16. function Display()
  17. {
  18. foreach($this->TestArray as $value)
  19. {
  20. echo $value;
  21. }
  22. }
  23.  
  24. function setTestArray($inputArray)
  25. {
  26. if(is_array($inputArray))
  27. {
  28. $this->TestArray = $inputArray;
  29. }
  30. }
  31.  
  32. function echoArrayA1()
  33. {
  34. $a = array('a');
  35. foreach($a as $value)
  36. {
  37. echo "<br />" . $value;
  38. }
  39. }
  40.  
  41. function echoArrayA2()
  42. {
  43. $a[0] = 'b';
  44. foreach($a as $value)
  45. {
  46. echo "<br />" . $value;
  47. }
  48. }
  49. }
  50. ?>
Any Questions?
Last edited by R0bb0b; Jun 23rd, 2008 at 7:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: percent20 is an unknown quantity at this point 
Solved Threads: 0
percent20 percent20 is offline Offline
Light Poster

Re: Can't access array in a class

 
0
  #3
Jun 23rd, 2008
So you have to pass it an array? you can't just grab a global one?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Can't access array in a class

 
1
  #4
Jun 23rd, 2008
Originally Posted by percent20 View Post
So you have to pass it an array? you can't just grab a global one?
You have to declare it as global in the function, view the last function in the class
  1. <?
  2. $globalTestArray = array('Hello', ' ', 'World', '2');
  3. $objtest = new Test(array('Hello', ' ', 'World'));
  4. $objtest->echoArrayA1();
  5. $objtest->echoArrayA2();
  6.  
  7. class Test
  8. {
  9. var $TestArray = array();
  10.  
  11. function Test($inputArray)
  12. {
  13. $this->setTestArray($inputArray);
  14. $this->Display();
  15. $this->echoGlobalTestArray();
  16. }
  17.  
  18. function Display()
  19. {
  20. foreach($this->TestArray as $value)
  21. {
  22. echo $value;
  23. }
  24. }
  25.  
  26. function setTestArray($inputArray)
  27. {
  28. if(is_array($inputArray))
  29. {
  30. $this->TestArray = $inputArray;
  31. }
  32. }
  33.  
  34. function echoArrayA1()
  35. {
  36. $a = array('a');
  37. foreach($a as $value)
  38. {
  39. echo "<br />" . $value;
  40. }
  41. }
  42.  
  43. function echoArrayA2()
  44. {
  45. $a[0] = 'b';
  46. foreach($a as $value)
  47. {
  48. echo "<br />" . $value;
  49. }
  50. }
  51.  
  52. function echoGlobalTestArray()
  53. {
  54. global $globalTestArray;
  55. echo "<br />";
  56. foreach($globalTestArray as $value)
  57. {
  58. echo $value;
  59. }
  60. echo "<br />";
  61. }
  62. }
  63. ?>
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: percent20 is an unknown quantity at this point 
Solved Threads: 0
percent20 percent20 is offline Offline
Light Poster

Re: Can't access array in a class

 
0
  #5
Jun 23rd, 2008
ok didn't notice the global. Thanks works now.

On to the next problem.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Can't access array in a class

 
0
  #6
Jun 23rd, 2008
No problem
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2230 | Replies: 5
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC