What does the PHP syntax $var1->$var2 mean?

后端 未结 3 2035
半阙折子戏
半阙折子戏 2020-12-18 01:43

What is the explanation for the following syntax?

$var1->$var2 // Note the second $
3条回答
  •  离开以前
    2020-12-18 02:37

    It means dynamically query a property in an object.

    class A {
      public $a;
    }
    
    // static property access
    $ob = new A;
    $ob->a = 123;
    print_r($ob);
    
    // dynamic property access
    $prop = 'a';
    $ob->$prop = 345; // effectively $ob->a = 345;
    print_r($ob);
    

    so $var1 is an instance of some object, -> means access to a member of that object and $var2 contains the name of a property.

提交回复
热议问题