| | |
Can't access array in a class
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2005
Posts: 33
Reputation:
Solved Threads: 0
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?
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
php Syntax (Toggle Plain Text)
$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. Last edited by percent20; Jun 23rd, 2008 at 6:15 pm.
php Syntax (Toggle Plain Text)
<? $objtest = new Test(array('Hello', ' ', 'World')); $objtest->echoArrayA1(); $objtest->echoArrayA2(); class Test { var $TestArray = array(); function Test($inputArray) { $this->setTestArray($inputArray); $this->Display(); } function Display() { foreach($this->TestArray as $value) { echo $value; } } function setTestArray($inputArray) { if(is_array($inputArray)) { $this->TestArray = $inputArray; } } function echoArrayA1() { $a = array('a'); foreach($a as $value) { echo "<br />" . $value; } } function echoArrayA2() { $a[0] = 'b'; foreach($a as $value) { echo "<br />" . $value; } } } ?>
Last edited by R0bb0b; Jun 23rd, 2008 at 7:34 pm.
•
•
•
•
So you have to pass it an array? you can't just grab a global one?
php Syntax (Toggle Plain Text)
<? $globalTestArray = array('Hello', ' ', 'World', '2'); $objtest = new Test(array('Hello', ' ', 'World')); $objtest->echoArrayA1(); $objtest->echoArrayA2(); class Test { var $TestArray = array(); function Test($inputArray) { $this->setTestArray($inputArray); $this->Display(); $this->echoGlobalTestArray(); } function Display() { foreach($this->TestArray as $value) { echo $value; } } function setTestArray($inputArray) { if(is_array($inputArray)) { $this->TestArray = $inputArray; } } function echoArrayA1() { $a = array('a'); foreach($a as $value) { echo "<br />" . $value; } } function echoArrayA2() { $a[0] = 'b'; foreach($a as $value) { echo "<br />" . $value; } } function echoGlobalTestArray() { global $globalTestArray; echo "<br />"; foreach($globalTestArray as $value) { echo $value; } echo "<br />"; } } ?>
![]() |
Similar Threads
- Safe Array (C++)
- return array from a class (Java)
- implementing a two demensional array class (C++)
- Urgently! Need help for C++ class (C++)
- Return Array from C++ (C++)
- encapsulating an array in a class? (C#)
- Accessing functions from base class (C)
Other Threads in the PHP Forum
- Previous Thread: Product Option Combinations/Variations Grid
- Next Thread: Accessing Other databases other than MYSQL
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external fcc file files folder form forms forum freelancing function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction menu method mlm mod_rewrite multiple mysql neutrality oop pageing pagerank paypal pdf php phpmysql play problem query question radio random recursion remote root script search select server sessions sms soap source space sql support! syntax system table template tutorial update upload url validator variable video web youtube





