How to check if a string can be used as a variable name in PHP?

前端 未结 7 1605
孤独总比滥情好
孤独总比滥情好 2021-02-15 18:08

In PHP one can use variable variables...

For example...

class obj { }
$fieldName = \"Surname\";
$object = new obj();
$object->Name = \"John\";
$objec         


        
7条回答
  •  终归单人心
    2021-02-15 18:42

    but I will not be able to access it with $object->...... because it would not parse correctly

    but look:

    class A {}
    
    $varName = '!asd asd';
    $a = new A();
    $a->$varName = '1';
    echo "{$a->{'!asd asd'}}"; // 1
    

    Certainly not recommended but it can be done.

提交回复
热议问题