php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69921 function_exists returns true before function definition
Submitted: 2015-06-24 14:56 UTC Modified: 2015-06-24 17:58 UTC
From: alan at aondra dot com Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.10 OS: Any
Private report: No CVE-ID: None
 [2015-06-24 14:56 UTC] alan at aondra dot com
Description:
------------
---
From manual page: http://www.php.net/function.function-exists
---

The function 'function_exists' does not execute in the order in which the code is evaluated. Not only is this wrong, it allows for the possibility that code could be executed in the incorrect scope.

I saw this note on the documentation:

"A function name may exist even if the function itself is unusable due to configuration or compiling options (with the image functions being an example)."

I looked and found no such configuration or compiling option that would allow for this.

Test script:
---------------
<?php
    header('Content-Type: text/plain');

    $mystr = 'should not execute';

    if(function_exists('myfunc'))
        { myfunc($mystr); }

    $mystr = 'execute';

    function myfunc($str)
        { die($str); }

    myfunc($mystr);

Expected result:
----------------
execute

Actual result:
--------------
should not execute

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-24 14:57 UTC] alan at aondra dot com
-Summary: function_exists +Summary: function_exists fires before function definition
 [2015-06-24 14:57 UTC] alan at aondra dot com
Gave a better summary
 [2015-06-24 15:10 UTC] alan at aondra dot com
-Summary: function_exists fires before function definition +Summary: function_exists returns true before function definition
 [2015-06-24 15:10 UTC] alan at aondra dot com
.
 [2015-06-24 17:09 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-06-24 17:09 UTC] cmb@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

According to the documentation[1]:

| Functions need not be defined before they are referenced, [...]

function_exists() yields a result accoring to this, i.e. where
a function can be called, function_exists() returns true.

[1] <http://php.net/manual/en/functions.user-defined.php>
 [2015-06-24 17:58 UTC] alan at aondra dot com
1. It should be documented on the function_exists page that it does not work as would be expected of pretty much everything else.

2. Even if it is documented, its function still hinges on broken logic. Not that this is a first by any means. See:
    i.imgur.com/pyDTn2i.jpg
    i.imgur.com/40VnUX1.png

Saying "because you didn't read an unlinked document (existence not hinted) in the manual, and expect sane code, it's not a bug" doesn't mean it's not a bug. It's still a bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 02 02:01:29 2024 UTC