Re: Static Array declaration within class PHP5

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Marek wrote:
Hello

I'm trying to create a public static array within a class.(PHP5).

First - the following fails:

class yadayada { public static $tester[0]="something"; public static $tester[1]="something 1";

of course this doesn't (ok so its obvious to me)
first of its generally not good form to initialize a var in the class definition but rather do it with a function. often that would be the constructor - but that won't do it this time.


here is how I would do it (given the facts you provided):

class Yadda
{
	private static $myArr;

	public static function getArr()
	{
		if (!isset(self::$myArr)) {
			self::setupArr();
		}

		return self::$myArr;
	}

	private static function setupArr()
	{
		self::$myArr = array(
			0 => 'something',
			1 => 'something1',
			2 => 'something2',
			3 => 'something3',
			4 => 'something4',
			5 => 'something5',
			6 => 'something6',
			7 => 'something7', // notice the trailing comma!
		);
	}
}

print_r( Yadda::getArr() );



btw: I did not syntax check this code.


Second - However the following works:

class yadayada { public static $tester = array (0 => "something", 1 >="something 1");

you are not forced to write the whole array on 1 line :-) also if you are using numeric keys there is no need to write them...:

$tester = array ("something", "something 1");

with or with out the keys its the same (assuming you keys start at zero and are always consecutive)



My question is this: I have an array that has about some 150 different values and using the second example would get very very messy, is there another method of doing this ? The first example(which fails) is the cleanest, is there anything I can do ?

by clean do you mean readable per chance? how readable is the class I just defined according to you?



Thank you



-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux