dynamic class names in php

前端 未结 7 1606
-上瘾入骨i
-上瘾入骨i 2020-12-07 00:31

I have a base class called field and classes that extend this class such as text, select, radio, checkbox, <

7条回答
  •  有刺的猬
    2020-12-07 00:55

    This should work to instantiate a class with a string variable value:

    $type = 'Checkbox'; 
    $field = new $type();
    echo get_class($field); // Output: Checkbox
    

    So your code should work I'd imagine. What is your question again?

    If you want to make a class that includes all extended classes then that is not possible. That's not how classes work in PHP.

提交回复
热议问题