Page 1 of 1

Can I make global array to private class array member?

Posted: Wed Feb 14, 2007 6:11 am
by nadavvin
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?

Posted: Wed Feb 14, 2007 6:12 am
by Jenk
:?:

post your existing code, please.

Posted: Wed Feb 14, 2007 6:26 am
by Chris Corbyn
If you do something like this the variable will be local, not global:

somefile.inc.php

Code: Select all

<?php

$whatever = array("foo", "bar");
SomeClass.php

Code: Select all

<?php

class SomeClass
{
    private $whatever = array();
    
    public function __construct()
    {
        include "somefile.inc.php";
        $this->whatever = $whatever;
    }
}
You can even do this and eliminate the var completely!

somefile.inc.php

Code: Select all

<?php return array("foo", "bar");
SomeClass.php

Code: Select all

<?php

class SomeClass
{
    private $whatever = array();
    
    public function __construct()
    {
        $this->whatever = include("somefile.inc.php");
    }
}