This is a "testing of the waters" RFC.  If there is interest, it will be
followed with a patch.  It should be noted that the patch for this has
been available through the various vortexes of namespace syntax for over
a year now, and it is an extremely simple patch.

[RFC]
Implement importing of functions to complement importing of classes and
namespaces.

example:

<?php
namespace foo;
include 'blah.php'; // defines function bar in namespace oof

use function oof\bar;
use function \strlen;

bar(); // calls oof\bar()
$a = strlen('hi'); // calls global function strlen
?>

[Problems this solves/Use cases]
(1) Readability:
* can make code more readable, much like importing classes/namespaces,
by shortening line lengths and simplifying complex compound expressions

(2) Backwards/Forwards compatibility
* Occasionally, an internal function is extended with additional
parameters.  With function import, this can be accomplished in old versions.

Example (pretend parameter "encoding" is added to strlen() in PHP 6.0):

<?php
namespace Util {
function strlen($string, $encoding = 'ISO-8559-1')
{
    $a = phpversion();
    if ($a[0] >= 6) {
        return \strlen($string, $encoding); // use internal implementation
    }
    // now for the simulated parameter
    switch ($encoding) {
        ...
    }
}
}

use function Util\strlen;
// now code following will work in all PHP versions, and is
forward-compatible to PHP 6
?>

[Proposed syntax changes]

Function import is accomplished with the keyword "function" (T_FUNCTION
token) as in

T_USE T_FUNCTION [use_statement]

use_statement can be any of \func or nsname\func or \nsname\func

Samples:

<?php
use function \globalfunc;
use function nsname\func;
use function \nsname\func;
?>

Just like the regular implementation of use for classes/namespaces:

<?php
use function globalfunc;
?>

will raise a warning that this has no effect (import in global scope of
unqualified name is meaningless).

<?php
namespace inside\namespace;
use function globalfunc;
?>

will act like:

<?php
namespace inside\namespace;
use function \globalfunc;
?>

[Drawbacks]
1) requires changing the engine near beta
2) adds new syntax to namespaces

[Non-issues/Slight issues]
1) would introduce one hash lookup on unqualified function calls at
compile-time if any "use" statement exists in the script, which is
unlikely to be detectable as a performance difference for even the
largest scripts.

Thanks,
Greg

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to