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!
EVERAH | PLEASE USE PHP OR CODE BBCODE TAGS WHEN POSTING CODE IN THE FORUMS.
Hello,
any one could help me ?
I'm trying to get the value of a static var by passing its name in a method (getString($varname)), but for the moment I've not found the correct way, it only returns the text "Texts::$first" but not its value.
Anyone know what the problem is ?
class Texts {
public static $first = "hello";
}
class One {
public static function getString($varname) {
$x = "Texts::$$varname";
return (string) $x;
}
}
@echo One::getString("first");
// The output should be "hello", but it does not work and returns "Texts::$first"
Thanks !
Last edited by RobertGonzalez on Fri Jan 16, 2009 12:21 pm, edited 1 time in total.
Reason:bbCode tags were not used
<?php
class MyTest {
public static $myvar = 'foo';
}
class MyNext {
public static function get($var) {
$val = ${$var}; // Braces not required, but a little easier to read
return MyTest::$val;
}
}
var_dump(MyNext::get('myvar'));
?>
thank you for your answer and sorry to not make use of the tags, next time I'll use them.
Using the code you show me still giving the following error :
Fatal error: Access to undeclared static property: MyTest::$val in ....
class MyTest {
public static $myvar = 'foo';
}
class MyNext {
public static function get($var) {
return MyTest::${$var};
}
}
echo MyNext::get('myvar');