Re: runtime access to static variable

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

 



Jack Bates wrote:
How do I access a static variable when I do not know the name of the
class until runtime?

I have the following example PHP:

ket% cat test.php <?php

class Test
{
  public static
    $STEPS = array(
      'foo',
      'bar');
}

$className = 'Test';

var_dump($className::$STEPS);
ket%
Unfortunately when I run it I get:

ket% php test.php
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
in /home/jablko/trash/test.php on line 13
ket%
I can call a static function using call_user_func(array($className,
'functionName')), and I can access a class constant using
constant($className.'::CONSTANT_NAME'). How do I access a static
variable?


this is also a not great but usable, stricter and faster solution:

<?php

class TestClass {
	
	public static $STEPS = array( 'foo' , 'bar' );
	
	public function steps() {
		return self::getSteps();
	}
	
	public static function getSteps() {
		return self::$STEPS;
	}
	
	public static function setSteps( $steps ) {
		self::$STEPS = $steps;
	}
}

$testClass = new TestClass;

print_r( $testClass->steps() );

?>

really though why..?

--
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