In PHP 5 can I instantiate a class dynamically?

前端 未结 4 485
予麋鹿
予麋鹿 2021-01-07 15:51

Is it possible to dynamically instantiate a class using a variable? For example is something like this possible in PHP?

class foo
{
    public $something;
}
         


        
相关标签:
4条回答
  • That should work, yes.

    You can also do:

    $f = new $class($arg1,$arg2);
    
    0 讨论(0)
  • 2021-01-07 16:29

    Yes, this code will work fine.

    0 讨论(0)
  • 2021-01-07 16:47

    Yes of course you can instantiate using dynamic names;

    0 讨论(0)
  • 2021-01-07 16:51

    In PHP 5 can I instantiate a class dynamically?

    Yes you can, your code should work fine.

    0 讨论(0)
提交回复
热议问题