JasonDavis
JasonDavis

Reputation: 48933

Can you define your own Namespace Separator in PHP?

Example using Namespace in PHP...

use \MyLibrary\Registry;
use \MyLibrary\User;
use \MyLibrary\Request;

namespace MyLibrary\Base
{
    class Base
    {
        public $registry;

        function __construct($registry)
        {
            $this->registry = $registry;
            $this->user = New User;
            $this->request = new Request;
            # code...
        }
    }
}

Now in C# the Namespace separator is more like this...

using MyLibrary.Registry;
using MyLibrary.User;
using MyLibrary.Request;

namespace MyLibrary.Base
{
    public partial class Base
    {
        public MyFunction(registry)
        {
            Code here
        }
    }
}

So I know there is a lot of complaints on the PHP namespace separator, so bad that some people don't even use them because they hate the current backslash.

So I am curious, is there any way possible to Define your own Namespace separator to be used in PHP?

Upvotes: 2

Views: 331

Answers (2)

mario
mario

Reputation: 145482

No is not an answer. It's just a matter of overhead of course. Apart from the mentioned redeclaration at compile time, you could use a preprocessor for the desired effect. (Just the effect, not an actual language change.)

There is for exmaple pihipi which defined a standard C++ esque namespace separator before PHP 5.3 did. And as second example there is the phpp preprocessor, albeit that only allows to enable/disable namespaces.

Using . like in Java-class languages is a bit ambigious and cannot be reasonably emulated in a preprocessor. It's borderline feasible at runtime, but not in PHP, and not without concessions about the distinct identifier planes. Using :: would be doable, but nobody has bothered yet. (Maybe the Quercus PHP implementation tried, not sure.)

Upvotes: 2

GordonM
GordonM

Reputation: 31730

I'm afraid you can't, it's hard-wired into the language. Such a thing would be really difficult to do anyway, as other symbols already have meanings assigned to them in PHP (. is string concatinate, / is divide, * is multiply, etc).

Upvotes: 5

Related Questions