Re: Scope of the variables around PHP class
| From: | Chris | Date: | Tue, 23 Sep 2008 01:13:07 +0000 |
| Subject: | Re: Scope of the variables around PHP class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-280747@lists.php.net to get a copy of this message | ||
VamVan wrote:
Hello Guys, I have a problem here. I hope you can help me resolving it. Please see the code below array.php has $array1 = ('hello'=>'heelo',) require_once('array.php'); class Classa { } How can I access the array values in my class? I want to understand the scope.You can't, unless you make $array a global variable which is not a proper solution. You could do something like this: class MyClass { private $my_array = array(); function __construct() {
require_once ('array.php');
$this->my_array = $array1;
}
}
Then you can use $this->my_array inside the class and it's automatically loaded when you create a new object.
--
Postgresql & php tutorials
http://www.designmagick.com/