Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP object properties

Tags:

properties

php

I'm new to OOP in PHP and I find the difference between the following two expressions difficult to understand.

    $object->$foo;
    $object->foo;

Maybe it's my fault, but I could not find the relevant part in the manual.

like image 914
martjahu Avatar asked Jul 31 '26 15:07

martjahu


1 Answers

The first call $obj->$foo is using a so called variable variable. Check this:

class A { 
    public $foo = 1;
}

$a = new A();
$foo = 'foo';

// now you can use both
echo $a->$foo;
echo $a->foo;

Follow the manual about variable variables

like image 130
hek2mgl Avatar answered Aug 03 '26 03:08

hek2mgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!