Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Namespaces in a single file with a global namespace

Tags:

namespaces

php

I have a file that require()'s a namespace, as such:

<?php
require_once('Beer.php');   // This file contains the Beer namespace

$foo = new Beer\Carlsburg();
?>

I would like to put the Beer namespace directly in the same file, like this (unworking) example:

<?php
namespace Beer {
    class Carlsburg {}
}

$foo = new Beer\Carlsburg();
?>

However, the PHP interpreter complains that No code may exist outside of namespace. I can therefore wrap $foo declaration in a namespace, but then I must also wrap Beer in that namespace to access it! Here is a working example of what I am trying to avoid:

<?php
namespace Main\Beer {
    class Carlsburg {}
}

namespace Main {
    $foo = new Beer\Carlsburg();
}
?>

Is there any way to include the code for the Beer namespace in the file, yet not wrap the $foo declaration in its own namespace (leave it in the global namespace)?

Thanks.

like image 225
dotancohen Avatar asked Feb 14 '12 09:02

dotancohen


People also ask

Can we declare multiple namespaces in same file?

Defining multiple namespaces in the same file ¶ Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.

What is global namespace in PHP?

Global space ¶ Without any namespace definition, all class and function definitions are placed into the global space - as it was in PHP before namespaces were supported. Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.

Can a program have multiple namespaces?

You can have the same name defined in two different namespaces, but if that is true, then you can only use one of those namespaces at a time. However, this does not mean you cannot use the two namespace in the same program. You can use them each at different times in the same program.

How do I use global namespace?

You can access the global namespace if you use brackets like this: namespace My\Space { function myScopedFunction() { .. } } namespace { function myGlobalFunction() { .. } }


4 Answers

You should use the global namespace :

<?php
namespace Beer {
    class Carlsburg {}
}


namespace { // global code
    $foo = new Beer\Carlsburg();
}
?>

See here -> http://php.net/manual/en/language.namespaces.definitionmultiple.php

like image 135
Manse Avatar answered Oct 24 '22 20:10

Manse


Try this

namespace Beer {
  class Carlsburg {}
}

//global scope 
namespace {
  $foo = new Beer\Carlsburg();
}

As per example #3 in Defining multiple namespaces in the same file

like image 36
Dmitry Kudryavtsev Avatar answered Oct 24 '22 20:10

Dmitry Kudryavtsev


Try placing a backslash before the namespace name:

$beer = new \Beer\Carlsberg();

The initial backslash is translated to "global namespace". If you do not put the leading backslash, the class name is translated to the current namespace.

like image 39
mingos Avatar answered Oct 24 '22 22:10

mingos


Just write it, it has no "name":

<?php
namespace Main\Beer {
    class Carlsburg {}
}

namespace {
    use Main\Beer;
    $foo = new Beer\Carlsburg();
}
?>

Demo and see Defining multiple namespaces in the same fileDocs.

like image 40
hakre Avatar answered Oct 24 '22 21:10

hakre