php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #28211 function_exists() returns false on static functions; method_exists() doesn't wor
Submitted: 2004-04-29 00:46 UTC Modified: 2004-04-30 18:59 UTC
From: gk at proliberty dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.3.4 OS: linux red hat 9
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-04-29 00:46 UTC] gk at proliberty dot com
Description:
------------
function_exists() returns false on static functions
method_exists() won't work without an instance of object

many times i have a deep class hierarchy and subclasses need to call inherited static methods. instances of each ancestor class in the hierarchy do not generally exist so method_exists() doesn't solve the problem.

If this is not a bug but a feature request, then it is also a documentation bug since the manual does not mention that function_exists() does not work with class member functions, which are truly 'functions', not 'methods'. 'method' assumes a class instance.

[greg@p3 test]$ php -v
PHP 4.3.2-RC (cli) (built: Apr 25 2003 18:03:38)



Reproduce code:
---------------
<?php
class a {
    function a(){
    }
    function static_func(){
       echo ("a::static_func() - the static function really exists!\n"); 
    }
}
a::static_func();
$function_exists = function_exists("a::static_func") ? 'exists': 'does not exist';
echo ("function_exists(\"a::static_func\") thinks that: \n");
echo("a::static_func() $function_exists\n"); 
$function_exists = function_exists("static_func") ? 'exists': 'does not exist';
echo ("function_exists(\"static_func\") thinks that: \n");
echo("static_func() $function_exists\n"); 
?>


Expected result:
----------------
[greg@p3 test]$ php test.php
a::static_func() - the static function really exists!
function_exists("a::static_func") thinks that: 
a::static_func() exists
function_exists("static_func") thinks that: 
static_func() does not exist


Actual result:
--------------

[greg@p3 test]$ php test.php
a::static_func() - the static function really exists!
function_exists("a::static_func") thinks that: 
a::static_func() does not exist
function_exists("static_func") thinks that: 
static_func() does not exist



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-29 15:22 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You need to use method_exists() for object methods. 
 [2004-04-29 21:52 UTC] gk at proliberty dot com
method_exists() is not applicable in this case since it only accepts an object, not the name of the class. There needs to be a way to check existing of static methods, in the absence of a class instance since you can call them and unavoidable fatal errors result if the function doesn't exist. This is a serious limitation in the PHP language. I don't know if PHP 5 addresses this.

Changed bug category to a feature request.
 [2004-04-30 17:35 UTC] brad at info-link dot net
try using "callable":

<?php
class A { function foo() { } }
echo "A::foo is_callable? ". is_callable(Array('A', 'foo')) ."\n";
?>

Outputs:

A::foo is_callable? 1

So basically, this request should be re-bogusified...
 [2004-04-30 18:59 UTC] derick@php.net
Agreed, is_callable is suited for this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jun 26 14:01:32 2024 UTC