php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11226 Unable to reference functions inside a class via "Variable Funcitons"
Submitted: 2001-05-31 18:32 UTC Modified: 2001-07-15 14:39 UTC
From: mikehatesspam at bigfoot dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.4pl1 OS: Linux 2.4.2 (Redhat Linux 7.1)
Private report: No CVE-ID: None
 [2001-05-31 18:32 UTC] mikehatesspam at bigfoot dot com
When I run this script I get this error message:

Fatal error: Call to undefined function: test::testfunc() in
/var/www/html/database/classes/Authenticator/test.php on line 9

I can execute the function by using test::testfunc();
directly, but not by using "Variable Functions".  I can also
change $function_name to be 'xyz' and it then works.

<?

class test {

	function calltest() {

		$function_name = 'test::testfunc';

		$function_name();

	}

	function testfunc() {

		echo "This now works\n";

	}
}

function xyz() {

	echo "This always works\n";
}

$t = new test;
$t->calltest();

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-18 10:16 UTC] kalowsky@php.net
i do believe you need to declare the function testfunc() BEFORE you try to reference it.  try that and see if it works.
 [2001-06-18 11:00 UTC] mikehatesspam at bigfoot dot com
I modified the code like this:

<?

function xyz() {

        echo "This always works";
}

class test {

        function testfunc() {

                echo "This now works";
        }

        function calltest() {

                $function_name = 'test::testfunc';

                $function_name();

        }
}

$t = new test;
$t->calltest();

?>

I got the exact same error.  I just don't think it can find functions inside a class.
 [2001-07-15 14:40 UTC] zeev@php.net
Indirect reference was not supported in the :: operator.  I added support so that the method name can be indirectly referenced (e.g., foo::$bar()), but the kind of indirect reference you attempted is not supported.

I don't see this type of indirection being supported in the future, as it essentially requires invoking the compiler over again on the string (because you use an operator, ::, in your name expression).  If you really need this type of functionality, use eval() instead.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 08:01:28 2024 UTC