Re: "PHP" namespace?
Hi,
On Mon, Jul 28, 2014 at 5:58 AM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> Hi all,
>
> Since we have discussion for Next PHP, "PHP" namespace discussion would be
> nice
> to have.
>
> Currently, PHP module functions/classes/interfaces are using global(root)
> namespace.
> If it is changed to use its own namespace, user space APIs may be changed
> flexible and user controlled manner. Thus, PHP may have
>
> - Consistent naming
> - Consistent parameter order
> - Graceful function/class/interface deprecation
> (We know what we should do for these, right?)
>
> without much compatibility issues.
>
>
> "PHP" namespace may be used to provide PHP(and 3rd party)
> functions/classes/interfaces/constants.
> "PHP\Legacy" (or whatever) may be used for legacy
> functions/classes/interfaces/constants.
>
> From PHP 5.6, userland function aliasing can be done with namespace.
> Following code overrides existing function.
>
> <?php
> use function \mb_strlen as strlen;
> var_dump(strlen('日本語'));
> ?>
>
> Result:
> int(3)
>
> It's good to use prefered API, but it requires "use" for every function
> APIs to be overridden.
> Note: Classes/Interfaces may override by "use" one by one also.
>
> With "PHP" and "PHP\Legacy" namespace, user may write:
>
> <?php
> namespace PHP; // Use current PHP functions/classes/interfaces/constants
>
> // Code uses current API
> ?>
>
> <?php
> namespace PHP;
> namespace PHP\Legacy; // Override with legacy PHP
> functions/classes/interfaces/constants.
>
> // Code uses legacy API
> ?>
>
> For compatibility, default namespace setting would be nice to have.
> - None for compiled default
> - "PHP" for php.ini-*
>
> Previous example codes became:
>
> <?php
> // Code uses current API
> ?>
>
> <?php
> namespace PHP\Legacy; // Override with legacy PHP
> functions/classes/interfaces/constants.
> // Code uses legacy API
> ?>
>
> Issue would be codes that assume PHP functions/classes/interfaces/constants
> are
> defined in global(root) namespace. (e.g. \strlen()) This could be
> workaround by allowing
> "use" aliasing to global(root) namespace. (e.g. "use PHP\Legacy as \;") In
> this case,
> current functions/classes/interfaces may stay in global(root) namespace.
> (or "use PHP as \;")
>
>
> Programmers may shoot their own foot by this. This is the trade off of
> flexibility.
>
> Any thoughts and/or comments?
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
This would be a major BC break, that couldn't possibly happen in PHP
5.x and IMO is way too radical even for PHP 6/7.
I generally hate to see statements like "this is not the PHP way", but
that's exactly the case with your proposal - it _enforces_ a more
strict way of programming. One must absolutely not be required to use
namespaces for everything.
Cheers,
Andrey.
Thread (17 messages)