# Allow functions in namespaces?

- Thread: https://externals.io/message/17857
- Original archive: https://news-web.php.net/php.internals/17857
- Started by Jessie Hernandez on 2005-08-12T03:50:08+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #17857 — Jessie Hernandez — 2005-08-12T03:50:08+00:00

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

Right now, my patch is complete as respects classes inside namespaces,
simple import, namespace imports, etc. I just made a few changes locally to
see how feasible it was to allow functions inside namespaces, and it was
actually very simple. Like classes, the functions internally are prefixed
with the namespace name and a colon, e.g.:

<?php
namespace test_ns
{
    function test_func
    {
        echo "hello!\n";
    }
}

test_func(); // generates an error, 'test_func' does not exist

test_ns:test_func(); // prints "hello!"
?>


I haven't done anything with functions as regards imports, and I think it
would be difficult/useless to do so. Simple imports might be easy, but what
about namespace imports? There is no such concept as __autoload for
functions, so namespace imports for functions would be discarded. Also, if
you import foo:bar, you don't know if this is a function or a class. The
former syntax of "import function/class foo from bar" would need to be
used, and this syntax was very ugly IMHO.

So, I ask, would it be useful to have functions inside namespaces and only
use it as above? Imports would only work for classes, and the only change
would be that you can reference functions with a colon (of course, just
like classes, you cannot declare a function name with a colon, only
reference it).

Let me know what you guys think. My Beta 2 patch is working great as it is,
and I suspect the majority of the users who want namespaces is to simply
group/organize their classes anyways.


Regards,

Jessie Hernandez

---

## #17858 — Benjamin Muskalla — 2005-08-12T05:57:24+00:00

URL: https://externals.io/message/17857#17858 · In reply to: #17857

Hi Jessie

I think there is no need for functions in namespaces because if you want
to group functions, you can use a class.

I think the destination of the namespaces is just to group classes
together.

btw: nice patch :)


-- Benny


> Right now, my patch is complete as respects classes inside namespaces,
> simple import, namespace imports, etc. I just made a few changes locally to
> see how feasible it was to allow functions inside namespaces, and it was
> actually very simple. Like classes, the functions internally are prefixed
> with the namespace name and a colon, e.g.:
> 
> <?php
> namespace test_ns
> {
>     function test_func
>     {
>         echo "hello!\n";
>     }
> }
> 
> test_func(); // generates an error, 'test_func' does not exist
> 
> test_ns:test_func(); // prints "hello!"
> ?>
> 
> 
> I haven't done anything with functions as regards imports, and I think it
> would be difficult/useless to do so. Simple imports might be easy, but what
> about namespace imports? There is no such concept as __autoload for
> functions, so namespace imports for functions would be discarded. Also, if
> you import foo:bar, you don't know if this is a function or a class. The
> former syntax of "import function/class foo from bar" would need to be
> used, and this syntax was very ugly IMHO.
> 
> So, I ask, would it be useful to have functions inside namespaces and only
> use it as above? Imports would only work for classes, and the only change
> would be that you can reference functions with a colon (of course, just
> like classes, you cannot declare a function name with a colon, only
> reference it).
> 
> Let me know what you guys think. My Beta 2 patch is working great as it is,
> and I suspect the majority of the users who want namespaces is to simply
> group/organize their classes anyways.
> 
> 
> Regards,
> 
> Jessie Hernandez

---

## #17859 — Marcus Boerger — 2005-08-12T06:34:29+00:00

URL: https://externals.io/message/17857#17859 · In reply to: #17857

Hello Jessie,

  don't overcomplicate it, just stay with classes.

marcus

Friday, August 12, 2005, 5:50:08 AM, you wrote:

> Right now, my patch is complete as respects classes inside namespaces,
> simple import, namespace imports, etc. I just made a few changes locally to
> see how feasible it was to allow functions inside namespaces, and it was
> actually very simple. Like classes, the functions internally are prefixed
> with the namespace name and a colon, e.g.:

> <?php
> namespace test_ns
> {
>     function test_func
>     {
>         echo "hello!\n";
>     }
> }

> test_func(); // generates an error, 'test_func' does not exist

> test_ns:test_func(); // prints "hello!"
?>>


> I haven't done anything with functions as regards imports, and I think it
> would be difficult/useless to do so. Simple imports might be easy, but what
> about namespace imports? There is no such concept as __autoload for
> functions, so namespace imports for functions would be discarded. Also, if
> you import foo:bar, you don't know if this is a function or a class. The
> former syntax of "import function/class foo from bar" would need to be
> used, and this syntax was very ugly IMHO.

> So, I ask, would it be useful to have functions inside namespaces and only
> use it as above? Imports would only work for classes, and the only change
> would be that you can reference functions with a colon (of course, just
> like classes, you cannot declare a function name with a colon, only
> reference it).

> Let me know what you guys think. My Beta 2 patch is working great as it is,
> and I suspect the majority of the users who want namespaces is to simply
> group/organize their classes anyways.


> Regards,

> Jessie Hernandez




Best regards,
 Marcus

---

## #18153 — Andi Gutmans — 2005-08-16T01:15:31+00:00

URL: https://externals.io/message/17857#18153 · In reply to: #17857

I definitely prefer sticking to just classes as I agree this is what most 
people really need it for (and it wouldn't get us into the trouble we got 
into when we tried to achieve the holy grail of namespaces).
Can you please send me an updated version of the patch to review?

At 08:34 AM 8/12/2005 +0200, Marcus Boerger wrote:
>Hello Jessie,
>
>   don't overcomplicate it, just stay with classes.
>
>marcus
>
>Friday, August 12, 2005, 5:50:08 AM, you wrote:
>
> > […]
>
> > […]
>
> > […]
>
> > […]
>?>>
>
>
> > […]
>
> > […]
>
> > […]
>
>
> > […]
>
> > […]
>
>
>
>
>Best regards,
>  Marcus

---

## #18154 — Jessie Hernandez — 2005-08-16T01:32:05+00:00

URL: https://externals.io/message/17857#18154 · In reply to: #18153

Hello Andi,

Attached is the latest version of the patch and some test support files that
I had to zip up because I couldn't "cvs add" new directories.

Let me know what you think and of any improvements I could make to the code.
Thanks!


Best regards,

Jessie


Andi Gutmans wrote:

> I definitely prefer sticking to just classes as I agree this is what most
> people really need it for (and it wouldn't get us into the trouble we got
> into when we tried to achieve the holy grail of namespaces).
> Can you please send me an updated version of the patch to review?
> 
> At 08:34 AM 8/12/2005 +0200, Marcus Boerger wrote:
> > […]
