943,917 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 4833
  • PHP RSS
Jun 23rd, 2008
0

Can't access array in a class

Expand Post »
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?

php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
percent20 is offline Offline
33 posts
since Jan 2005
Jun 23rd, 2008
0

Re: Can't access array in a class

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Jun 23rd, 2008
0

Re: Can't access array in a class

So you have to pass it an array? you can't just grab a global one?
Reputation Points: 12
Solved Threads: 0
Light Poster
percent20 is offline Offline
33 posts
since Jan 2005
Jun 23rd, 2008
1

Re: Can't access array in a class

Click to Expand / Collapse  Quote originally posted by percent20 ...
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
php Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Jun 23rd, 2008
0

Re: Can't access array in a class

ok didn't notice the global. Thanks works now.

On to the next problem.
Reputation Points: 12
Solved Threads: 0
Light Poster
percent20 is offline Offline
33 posts
since Jan 2005
Jun 23rd, 2008
0

Re: Can't access array in a class

No problem
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Product Option Combinations/Variations Grid
Next Thread in PHP Forum Timeline: Accessing Other databases other than MYSQL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC