| | |
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 |
# .htaccess 5.2.10 ajax apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date display dissertation dynamic echo email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link local login loop mail memberships menu mlm mod_rewrite multiple mysql mysqlquery oop open paypal pdf persist php problem query radio random recursion regex remote script search server sessions sms soap sockets source space spam sql syntax system table tutorial update upload url validation validator variable video web xml youtube





