# [RFC] Importing namespaced functions

- Thread: https://externals.io/message/68214
- Original archive: https://news-web.php.net/php.internals/68214
- Started by Igor Wiedler on 2013-07-19T17:29:24+00:00
- Messages: 37
- RFC: Importing namespaced functions (Implemented (PHP 5.6)) — https://externals.io/rfc/use_function.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/68214.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #68214 — Igor Wiedler — 2013-07-19T17:29:24+00:00

URL: https://externals.io/message/68214#68214

Hello internals,

I posted the initial idea for a use_function RFC a few months back. I would like to make the proposal official now, and open it for discussion.

I also did some work on a patch that probably still has some issues. Review on that is welcome as well.

RFC: https://wiki.php.net/rfc/use_function
Patch: https://github.com/php/php-src/pull/388

Regards,

Igor

---

## #68279 — Igor Wiedler — 2013-07-22T22:51:11+00:00

URL: https://externals.io/message/68214#68279

Hi Lars,

I don't really like the idea of such ghost features that don't actually do anything. It sounds like it would just introduce wrong mappings. And since those other constructs *do* share a namespace it technically wouldn't make sense to make a distinction between them.

Any other thoughts?

Regards,

Igor



> Hi Igor,
> 
> I like the proposal but one cosmetic thing: if we allow "use function …", shouldn't we allow "use class …"/"use abstract class …"/"use interface …"/"use namespace …" as well? It would be just syntactic sugar and not checked at all, but it would allow for nice symmetry in the use part of a file.
> 
> cu,
> Lars

---

## #68280 — Alexey Zakhlestin — 2013-07-23T06:20:51+00:00

URL: https://externals.io/message/68214#68280 · In reply to: #68214

> Hello internals,
> 
> I posted the initial idea for a use_function RFC a few months back. I would like to make the proposal official now, and open it for discussion.
> 
> I also did some work on a patch that probably still has some issues. Review on that is welcome as well.
> 
> RFC: https://wiki.php.net/rfc/use_function
> Patch: https://github.com/php/php-src/pull/388


I don't see much space for discussion :)
It's a very useful addition with nice explicit semantics and a small patch.

+1

-- 
Alexey Zakhlestin
CTO at Grids.by/you
https://github.com/indeyets
PGP key: http://indeyets.ru/alexey.zakhlestin.pgp.asc

---

## #68282 — Martin Keckeis — 2013-07-23T08:10:47+00:00

URL: https://externals.io/message/68214#68282 · In reply to: #68280

>
> > […]
> would like to make the proposal official now, and open it for discussion.
> > […]
> Review on that is welcome as well.
> > […]
>
>
 Hello,

some questions:
Alias is not required, but is it possible?

Would it be possible to write something like this?
<?php
namespace MySpace;

use OtherLib\HtmlResponse;
use function html\div, html\p, html\em;

class MyClass{

    public function getResponse(){
        return new HtmlResponse( div(p('Some', em('Text'))) );
    }
}
?>

Best regards
Martin

---

## #68284 — Igor Wiedler — 2013-07-23T10:44:24+00:00

URL: https://externals.io/message/68214#68284 · In reply to: #68282

Hi Martin,

Aliasing is possible, there is an example of it in the test cases of the patch [0].

As for your example, it's valid.

Regards,

Igor

[0]: https://github.com/igorw/php-src/blob/use-function/Zend/tests/use_function/alias.phpt



>  Hello,
> 
> some questions:
> Alias is not required, but is it possible?
> 
> Would it be possible to write something like this?
> <?php
> namespace MySpace;
> 
> use OtherLib\HtmlResponse;
> use function html\div, html\p, html\em;
> 
> class MyClass{
> 
>     public function getResponse(){
>         return new HtmlResponse( div(p('Some', em('Text'))) );
>     }
> }
> ?>
> 
> Best regards
> Martin

---

## #68285 — Sebastian Krebs — 2013-07-23T14:23:20+00:00

URL: https://externals.io/message/68214#68285 · In reply to: #68214

Hi,

Whats about constants?

Regards,
Sebastian
Am 19.07.2013 19:30 schrieb "Igor Wiedler" <igor@wiedler.ch>:

> Hello internals,
>
> I posted the initial idea for a use_function RFC a few months back. I
> would like to make the proposal official now, and open it for discussion.
>
> I also did some work on a patch that probably still has some issues.
> Review on that is welcome as well.
>
> RFC: https://wiki.php.net/rfc/use_function
> Patch: https://github.com/php/php-src/pull/388
>
> Regards,
>
> Igor

---

## #68286 — Igor Wiedler — 2013-07-23T16:50:28+00:00

URL: https://externals.io/message/68214#68286 · In reply to: #68285

Hi Sebastian,

That's a very good point. I will start working on adding support for `use const foo\BAR;`. It makes sense to support both.

Should this be done as a variant? Or can I just include it in the RFC directly?

Cheers,

Igor



> Hi,
> 
> Whats about constants?
> 
> Regards,
> Sebastian

---

## #68287 — Igor Wiedler — 2013-07-23T20:28:20+00:00

URL: https://externals.io/message/68214#68287 · In reply to: #68285

Hi,

Based on Sebastian's feedback I have updated the RFC and the patch to include a `use const` sequence that works just like `use function`, but for namespaced constants.

Example usage:

    namespace foo\bar {
        const baz = 42;
    }
     
    namespace {
        use const foo\bar\baz;
        var_dump(baz);
    }

I also fixed some other issues that the original patch had in the process (see commit history). Please keep the feedback coming.

Thanks,

Igor

---

## #68288 — Sebastian Krebs — 2013-07-23T20:42:53+00:00

URL: https://externals.io/message/68214#68288 · In reply to: #68287

Hi,

Thanks for that.

But actually I don't see: Why is "use" not enough? As far as I can see your
example, why it would introduce a BC, doesn't really match to original
question, why "use function" is used. Especially I don't see any ambiguity:

foo(); // Always a function. You cannot call classes this way
new foo(); // always a class
foo::bar(); // Always a class

But the example I mentioned before

namespace {
    function bar() {}}
 namespace foo {
    function bar() {}}
 namespace {
    use foo\bar;
    bar();}

points to a different problem, which I didn't see solved by adding the
"function"-keyword to "use". Can you clarify this? Because I'd try to avoid
new syntax wherever possible.

Regards,
Sebastian


2013/7/23 Igor Wiedler <igor@wiedler.ch>

> Hi,
>
> Based on Sebastian's feedback I have updated the RFC and the patch to
> include a `use const` sequence that works just like `use function`, but for
> namespaced constants.
>
> Example usage:
>
>     namespace foo\bar {
>         const baz = 42;
>     }
>
>     namespace {
>         use const foo\bar\baz;
>         var_dump(baz);
>     }
>
> I also fixed some other issues that the original patch had in the process
> (see commit history). Please keep the feedback coming.
>
> Thanks,
>
> Igor
>
>


-- 
github.com/KingCrunch

---

## #68289 — Stas Malyshev — 2013-07-23T21:15:30+00:00

URL: https://externals.io/message/68214#68289 · In reply to: #68288

Hi!

> But actually I don't see: Why is "use" not enough? As far as I can see your
> example, why it would introduce a BC, doesn't really match to original

Consider this:

use a\b\c as foo;

foo();

Now it would resolve to global function foo(). If use were changed to
apply to functions, it would resolve to function \a\b\c() instead.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #68290 — Igor Wiedler — 2013-07-23T21:18:37+00:00

URL: https://externals.io/message/68214#68290 · In reply to: #68288

Hi Sebastian,

The reason is precisely to avoid BC breaks, such as the one that you quoted from the FAQ.

Are you suggesting that such a BC break is acceptable for 5.6?

As much as I dislike introducing new syntax, keeping BC was the reason I did so.

---

## #68291 — Sebastian Krebs — 2013-07-23T21:30:04+00:00

URL: https://externals.io/message/68214#68291 · In reply to: #68289

2013/7/23 Stas Malyshev <smalyshev@sugarcrm.com>

> Hi!
>
> > […]
> your
> > […]
>
> Consider this:
>
> use a\b\c as foo;
>
> foo();
>
> Now it would resolve to global function foo(). If use were changed to
> apply to functions, it would resolve to function \a\b\c() instead.
>

But if I explicitly import "foo" this way, this is intended :?
Yes, "foo" could also be a classname, but right now it is also not part of
the language to detect collisions.
Additional I have _never_ seen any core- or user-function, whose name could
be a classname to. Of course I've never seen every function ever written,
but even if this a real issue, how many people are really affected?

Regards,
Sebastian.


> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>



-- 
github.com/KingCrunch

---

## #68292 — Sebastian Krebs — 2013-07-23T21:32:15+00:00

URL: https://externals.io/message/68214#68292 · In reply to: #68290

2013/7/23 Igor Wiedler <igor@wiedler.ch>

> Hi Sebastian,
>
> The reason is precisely to avoid BC breaks, such as the one that you
> quoted from the FAQ.
>
> Are you suggesting that such a BC break is acceptable for 5.6?
>

I don't fear BCs and actually I prefer BCs against weak compromises. This
depends on the weight of the BC ;)

But aside: I am a regular user, not a core-dev or something. I just comment.


>
> As much as I dislike introducing new syntax, keeping BC was the reason I
> did so.
>
>


-- 
github.com/KingCrunch

---

## #68293 — Stas Malyshev — 2013-07-23T22:00:33+00:00

URL: https://externals.io/message/68214#68293 · In reply to: #68291

Hi!

> But if I explicitly import "foo" this way, this is intended :? 

No, it's a BC break and as such is not acceptable.

> Additional I have _never_ seen any core- or user-function, whose name
> could be a classname to. Of course I've never seen every function ever
> written, but even if this a real issue, how many people are really affected?

It is a BC break and as such it is a real issue.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #68294 — Yasuo Ohgaki — 2013-07-24T00:32:44+00:00

URL: https://externals.io/message/68214#68294 · In reply to: #68214

Hi,

This patch good for extending PHP usability. IMHO.
Modern popular languages like Ruby and Javascript has way to extend/modify
existing object/method behaviors.
(Languages support like this: Smalltalk, JavaScript, Objective-C, Ruby,
Perl, Python, Groovy, and so on)

I just would like to point it out that there are many modern languages that
support similar feature. Therefore, I would like to see discussion rather
than whether this will or not will be part of PHP, but how this kind of
feature should be implemented.

Regards,


--
Yasuo Ohgaki
yohgaki@ohgaki.net


2013/7/20 Igor Wiedler <igor@wiedler.ch>

> Hello internals,
>
> I posted the initial idea for a use_function RFC a few months back. I
> would like to make the proposal official now, and open it for discussion.
>
> I also did some work on a patch that probably still has some issues.
> Review on that is welcome as well.
>
> RFC: https://wiki.php.net/rfc/use_function
> Patch: https://github.com/php/php-src/pull/388
>
> Regards,
>
> Igor

---

## #68295 — Stas Malyshev — 2013-07-24T04:34:05+00:00

URL: https://externals.io/message/68214#68295 · In reply to: #68294

Hi!

> This patch good for extending PHP usability. IMHO.
> Modern popular languages like Ruby and Javascript has way to extend/modify
> existing object/method behaviors.

PHP has it too, it is called runkit. However it makes reasoning about
the code much harder, so I do not think it is worth it to make it part
of standard PHP language. However, I do not see how this is related at
all to the RFC being discussed.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #68296 — Yasuo Ohgaki — 2013-07-24T05:28:22+00:00

URL: https://externals.io/message/68214#68296 · In reply to: #68295

Hi Stas,

2013/7/24 Stas Malyshev <smalyshev@sugarcrm.com>

> > […]
> extend/modify
> > […]
>
> PHP has it too, it is called runkit. However it makes reasoning about
> the code much harder, so I do not think it is worth it to make it part
> of standard PHP language. However, I do not see how this is related at
> all to the RFC being discussed.


RunKit is nice, but it's not a standard module and I suppose it will
not be standard.

This might be a poor example of usefulness of overriding existing
class/method, but proposed feature may be used for monkey patch
for instance.

http://en.wikipedia.org/wiki/Monkey_patch

There are issues as discussed in Wiki. So not having such
feature is valid option, but it's nice to have feature. Make `strlen()`
return number of chars instead of bytes, etc.

I don't have concrete idea right now. If there is cleaver way that
avoids disadvantages, I would like to see such feature by default
since almost all PHP competitors have this.

Any ideas that satisfy most of us?

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #68297 — Stas Malyshev — 2013-07-24T07:18:45+00:00

URL: https://externals.io/message/68214#68297 · In reply to: #68296

Hi!

> I don't have concrete idea right now. If there is cleaver way that
> avoids disadvantages, I would like to see such feature by default 
> since almost all PHP competitors have this.

We are not in competition with other languages over number of features
implemented. "All cool boys do it" is not really a good answer, the good
answer should be why is it good for PHP as a language and as an
ecosystem having particular target audience and particular purpose. In
my opinion, changing what classes do in runtime fits the PHP's
traditional audience and purposes rather poorly. Of course, there are
always non-traditional ways of using it, it is a time-honored way of
hackers. For them, there's always runkit.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #68298 — Igor Wiedler — 2013-07-24T11:03:09+00:00

URL: https://externals.io/message/68214#68298 · In reply to: #68296

Hi Yasuo,

Monkey patching could be a very useful feature, especially for functions in a testing context.

But I agree with Stas that it's not really related to importing function/class symbols. Perhaps you should create a separate RFC for it.



> RunKit is nice, but it's not a standard module and I suppose it will 
> not be standard.
> 
> This might be a poor example of usefulness of overriding existing 
> class/method, but proposed feature may be used for monkey patch
> for instance.
> 
> http://en.wikipedia.org/wiki/Monkey_patch
> 
> There are issues as discussed in Wiki. So not having such
> feature is valid option, but it's nice to have feature. Make `strlen()`
> return number of chars instead of bytes, etc.
> 
> I don't have concrete idea right now. If there is cleaver way that
> avoids disadvantages, I would like to see such feature by default 
> since almost all PHP competitors have this.
> 
> Any ideas that satisfy most of us?

---

## #68438 — Igor Wiedler — 2013-08-08T12:29:16+00:00

URL: https://externals.io/message/68214#68438 · In reply to: #68214

Hi everyone,

I just wanted to bump this topic, since there's not been much feedback during the last few weeks. Comments on the patch are also welcome.

RFC: https://wiki.php.net/rfc/use_function
Patch: https://github.com/php/php-src/pull/388

Thanks,

Igor

---

## #68446 — Robert Stoll — 2013-08-08T18:17:36+00:00

URL: https://externals.io/message/68214#68446 · In reply to: #68438

Hi Igor,

Personally I like your RFC. I also like the fact that it is possible to
"replace" built-in functions where desired by declaring a corresponding use
statement.
I do not see aliases in your RFC but I guess it would also be possible to
write things like: use function foo\bar as foo;

cheers,
Robert

-----Ursprüngliche Nachricht-----
Von: Igor Wiedler [mailto:igor@wiedler.ch] 
Gesendet: Donnerstag, 8. August 2013 14:29
An: internals@lists.php.net
Betreff: [PHP-DEV] Re: [RFC] Importing namespaced functions

Hi everyone,

I just wanted to bump this topic, since there's not been much feedback
during the last few weeks. Comments on the patch are also welcome.

RFC: https://wiki.php.net/rfc/use_function
Patch: https://github.com/php/php-src/pull/388

Thanks,

Igor


--
 To unsubscribe, visit:
http://www.php.net/unsub.php

---

## #68447 — Igor Wiedler — 2013-08-08T18:43:38+00:00

URL: https://externals.io/message/68214#68447 · In reply to: #68446

Yes, aliases are supported just as you would expect. I will add an example to the RFC.

Thanks,

Igor



> Hi Igor,
> 
> Personally I like your RFC. I also like the fact that it is possible to
> "replace" built-in functions where desired by declaring a corresponding use
> statement.
> I do not see aliases in your RFC but I guess it would also be possible to
> write things like: use function foo\bar as foo;
> 
> cheers,
> Robert

---

## #68448 — Michael Wallner — 2013-08-08T18:44:37+00:00

URL: https://externals.io/message/68214#68448 · In reply to: #68438

> Hi everyone,
>
> I just wanted to bump this topic, since there's not been much feedback during the last few weeks. Comments on the patch are also welcome.
>
> RFC: https://wiki.php.net/rfc/use_function
> Patch: https://github.com/php/php-src/pull/388
>

I like it. It improves the state for non-OOP in PHP.

-- 
Regards,
Mike

---

## #68455 — Daniel Bingham — 2013-08-08T20:30:47+00:00

URL: https://externals.io/message/68214#68455 · In reply to: #68448

Hey Igor,

I love the RFC, but how about:

use bar\baz() as baz;

As an alternative to "use function".  Still new syntax, but a little less
wordy.  I have nothing for "use const", but "use const" feels more natural
to me than "use function" does.

Just an idea for pondering!

Dan





> 
> > […]
> during the last few weeks. Comments on the patch are also welcome.
> > […]
>
> I like it. It improves the state for non-OOP in PHP.
>
> --
> Regards,
> Mike

---

## #68468 — Leigh — 2013-08-09T17:16:34+00:00

URL: https://externals.io/message/68214#68468 · In reply to: #68455

On Aug 8, 2013 9:31 PM, "Daniel Bingham" <daniel.bingham@ellislab.com>
wrote:
>
> Hey Igor,
>
> I love the RFC, but how about:
>
> use bar\baz() as baz;
>
> As an alternative to "use function".  Still new syntax, but a little less
> wordy.

The parenthesis add the feeling that it should be evaluated imho, I.e. the
result of that function should be used as baz. That could possibly lead to
confusion. I know you're suggesting as an additional syntax not
alternative, but I don't get good vibes from it.

Also might not even be possible due to how the parser handles parenthesis,
but I'd have to look over the parser grammar to be sure.

But another +1 from me on the RFC as a whole. Now its there I can't believe
it took so long.

---

Next page: https://externals.io/message/68214.md?page=2
