For some reason, I cannot get dynamic calls to class functions to work from either inside or outside the class. Here is a small sample code:
<?
class A
{
var $variable1;
function func1 ( $str)
{
print("<br>The value of the string is: " . $str);
}
function func2 ()
{
$tmp = "\$this->func1";
$tmp("Here is a string");
}
}
$obj = new A;
$obj->func1("Here is a string from outside the class"); // This works
$tmp = "\$obj->func1";
$tmp("Here is a second string"); // This call from outside the class fails
$obj->func2(); // This call from inside the class fail too.
/
When func2 tries to call func1, php generates the following error message:
Fatal error: Call to undefined function: $this->func1() in trialdynamicclass.php on line 13
A similar message is printed when I attempt to make the call from outside the class.
Note that I have tried this both with and without the $this prepended to the function name. The
results, in either case, lead to the same failure.
- Note, further, that if I put func2 INSIDE func1, i.e. in a structure like this:
*
- function func1 ( $str)
- {
- function func2 ()
- {
- ... function 2 code here
- }
*
- ... function 1 code here
*
- }
*
- then the dynamic call, (to func2, NOT to $this->func2 will work. HOWEVER, the
- code inside func2 will not be aware of class variables defined outside of func1.
- In other words, I will not be able to access $this->variable1.
/
?>