php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49100 Reference of native function in variable (function pointers)
Submitted: 2009-07-29 14:58 UTC Modified: 2021-10-21 12:02 UTC
Votes:4
Avg. Score:2.0 ± 1.7
Reproduced:2 of 3 (66.7%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: tom at r dot je Assigned: cmb (profile)
Status: Wont fix Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tom at r dot je
New email:
PHP Version: OS:

 

 [2009-07-29 14:58 UTC] tom at r dot je
Description:
------------
With the intorduction of closures it would be nice to be able to have some control over native functions for example:


$foo = function() { 
echo 'bar';
}

$foo();

Works fine. 


It would be nice to be able to do something like

function foo() {
echo 'bar';
}

$foo = &foo;
$foo();

And ultimately, override the defined function:

function foo() {
echo 'bar';
}

$oldFoo = &foo;
foo = function() use ($oldFoo) {
   $oldFoo();
   echo 'bar';
}


foo(); // will print 'foobar';

There are several uses for this, just look at the way closures are commonly used in javascript. It's really useful for debugging, as a function can be substituted easily.

Though this probably isn't possible due to the clear distinction php makes between functions and variables. 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 22:50 UTC] jani@php.net
-Summary: Reference Native Function in variable (function pointers) +Summary: Reference of native function in variable (function pointers) -Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: Windows Vista +Operating System: * -PHP Version: 5.3.0 +PHP Version: *
 [2021-10-21 12:02 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-10-21 12:02 UTC] cmb@php.net
You can already do this with uopz[1] and runkit7[2], and it is
highly unlikely that such functionality will ever make it into the
core.

> It's really useful for debugging, as a function can be
> substituted easily.

I suggest to use a debugger.

[1] <https://pecl.php.net/package/uopz>
[2] <https://pecl.php.net/package/runkit7>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 19:01:35 2025 UTC