I have class which have array.
Since the array is big, I want to separate the array to different file and "load" it to the class.
Normal include will make the array global variable, although I can pass it to the class, It still global variable.
How does I include array as a private field of a class?
Can I make global array to private class array member?
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
If you do something like this the variable will be local, not global:
somefile.inc.php
SomeClass.php
You can even do this and eliminate the var completely!
somefile.inc.php
SomeClass.php
somefile.inc.php
Code: Select all
<?php
$whatever = array("foo", "bar");Code: Select all
<?php
class SomeClass
{
private $whatever = array();
public function __construct()
{
include "somefile.inc.php";
$this->whatever = $whatever;
}
}somefile.inc.php
Code: Select all
<?php return array("foo", "bar");Code: Select all
<?php
class SomeClass
{
private $whatever = array();
public function __construct()
{
$this->whatever = include("somefile.inc.php");
}
}