Forum Moderators: coopster
This doens't make sense to me:
<?php
class Foo
{
function Variable()
{
$name = 'Bar';
$this->$name(); // This calls the Bar() method
}
function Bar()
{
echo "This is Bar";
}
}
$foo = new Foo();
$funcname = "Variable";
$foo->$funcname(); // This calls $foo->Variable()
?>
How is $funcname(); called as a method of $foo when the class defintion doesn't contain $funcname() as a function/method?
function Variable()
{
$name = 'Bar';
$this->$name(); // This calls the Bar() method
}
So PHP will render the variable first, thereby making it a variable function and calling ...
$foo->$funcname();
$foo->Variable();
Using the -> to me means that you are either assigning a variable that is defined IN the class, or you are calling a function/method of the class.
How $foo->$funcname() works is beyond me, but I do understand what is happening behind the scenese.
Ok as I right this post it is becoming clearer, basically $funname() gets replaced by Variable since that is what was assigned to the 'variable'.
Still kinda 'hacky' to me hehe.