how to use variable as part of class name?

Discussion in 'PHP' started by aayybb, Oct 4, 2010.

  1. #1
    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.
     
    aayybb, Oct 4, 2010 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    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:
     
    joebert, Oct 4, 2010 IP
  3. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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);
     
    aayybb, Oct 5, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    // Edit -- I stand corrected
     
    Last edited: Oct 5, 2010
    joebert, Oct 5, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    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?
     
    lukeg32, Oct 5, 2010 IP
  6. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #6
    
    $returnValue = call_user_func_array(array($classNameOrObjectInstance,$methodname), array($param1, $param2));
    // or
    $returnValue = call_user_func_array('ClassName::methodName', array($param1, $param2));
    
    PHP:
     
    Gray Fox, Oct 5, 2010 IP
  7. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This works:)
    call_user_func_array('ClassName::methodName', array($param1, $param2));
     
    aayybb, Oct 6, 2010 IP