Reputation: 15
Firstly here is a code example which works:
<?php
class Foo
{
private $name = "test";
public function __construct()
{
$name = $this->name;
$this->$name();
}
function test()
{
echo "test";
}
}
$foo = new Foo();
?>
Now, my question is: Is it possible to directly use the artribute name of the class Foo to call the method test without creating a new variable like this:
<?php
class Foo
{
private $name = "test";
public function __construct()
{
$this->$name();
}
function test()
{
echo "test";
}
}
$foo = new Foo();
?>
Looking forward for your answers, Heeiiigou
Upvotes: 0
Views: 362
Reputation: 147
Wrap it in {} like so:
function __construct() {
$this->{$this->name}();
}
Upvotes: 1