Can I make global array to private class array member?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Can I make global array to private class array member?

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

:?:

post your existing code, please.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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");
    }
}
Post Reply