3

So I'm targetting my compiler to PHP and I'm having some problems with namespaces.

They look something like this in my language:

package Foo
{
    package Bar
    {
        class X { }
    }

    class Y { }
}

Of course, PHP doesn't handle nested namespaces. What's the best way to translate this code so it still works in PHP?

I should note that all files are compiled into a single PHP file in the end. One caveat of this is that I'll have to go back to the global namespace after I've closed a package, and I haven't found any documentation about how to do that. It seems to me that in PHP, once you declare a namespace, it applies to the whole file.

2
  • php.net/manual/en/language.namespaces.definitionmultiple.php Commented Dec 21, 2014 at 9:28
  • Okay so I don't have a perfect solution that maybe others would be looking for, but I have something that works. Obviously compiled code doesn't have to exactly represent the source code, and it doesn't have to be perfectly readable, so I've decided the simplest solution is to embed the namespaces into the classes themselves, i.e. class Foo__Bar__X { } class Foo_Y { } $f = new Foo__Bar__X(); It works, it's simple and I don't have to completely rearrange the order that classes are compiled. Commented Dec 21, 2014 at 14:31

2 Answers 2

10

It's true that "Namespace declarations cannot be nested".Namespacing is used to avoid conflicting definitions and introduce more flexibility and organization in your code.The brackets surrounding the namespace code block are completely optional.If you want to translate your code to run able in php. You can write like:

namespace Foo\Bar;

class X{
    // Your properties
}
class Y {
// Your properties
}

If you need more information you can see this link well written by Elias Zerrouq with example http://code.tutsplus.com/tutorials/namespacing-in-php--net-27203

Sign up to request clarification or add additional context in comments.

2 Comments

Notice however that class X is inside Foo.Bar, whereas class Y is just inside Foo.
php.net/manual/en/language.namespaces.importing.php from here you will know the Aliasing/Importing which class you want to write after your 1st class.
2

Zotoaster it is quite possible to do what you desire; however, my solution would rely on the psr-4 autoloading solution. With this, by placing your files in the appropriate folders, you would be able to do the following at the start of the php file:

use Foo\Bar\X;
use Foo\Y;

Thereafter you can instantiate the classes accordingly:

$x = new X();
$y = new Y();

You can read more on how to use namespaces here: http://www.php-fig.org/psr/psr-4/ Furthermore, you could use composer to handle the "dirty work" for you, i.e. all the auto loading: https://getcomposer.org/doc/01-basic-usage.md#autoloading

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.