Hi, I would like to use a variable as part of class name. Wonder how to do it. $classname = 'test'; do_something_$classname::dosomething( $params); This doesn't work. What is the correct way to implement the variable into a class name? Thanks for any help in advanced.
If you're using at least PHP 5.3 you can use something like this. <?php class my_test { public static function fun() { return 'fun'; } } $whos = 'my'; $whos_fun = "{$whos}_test"; echo $whos_fun::fun(); ?> PHP:
Hi, I have to use php 5.2. I tried the following. The first one works but the second doesn't. It is calling the same thing but didn't getting the correct result when running the second way. Wonder why. This line works. test_Utils_Address_test::checkAddress( $params ); The following lines don't work. $something = 'test'; $className = "test_Utils_Address_". $something; call_user_func_array(array($className,"checkAddress"), $params);
You can invoke an object (using new) through a number of ways, such as variable variables and so on, which would work for you; However, if you need to access static methods then you will not be able to do it - unless you wish to use eval - using this - for any reason though - is very sloppy programming so you should avoid it at all costs...... is there any reason you can invoke the object first to do do what you are after?
$returnValue = call_user_func_array(array($classNameOrObjectInstance,$methodname), array($param1, $param2)); // or $returnValue = call_user_func_array('ClassName::methodName', array($param1, $param2)); PHP: