Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Root namespace

Tags:

namespaces

php

I have got a single file in which I need specify classes in multiple namespaces, something like:

<?php

namespace library;

class ClassInLib {
   ...
}

namespace \; //Switch to root namespace

class ClassInRoot {
   ...
}

The above code has a syntax error at namespace \;. How can I switch from the library namespace to the root namespace?

Why do I need this: I need to mock a bunch of classes during unit testing and I don't think these very short mock classes justify being in separate files.

like image 205
d_inevitable Avatar asked Nov 20 '25 10:11

d_inevitable


1 Answers

namespace 
{ 
    class RootClass
    {

        function whatever();
    }
}
namespace Symfony\Component\DependencyInjection
{
    interface ContainerAwareInterface
    {

        function setContainer(ContainerInterface $container = null);
    }
}

http://www.php.net/manual/en/language.namespaces.definitionmultiple.php

Good chance you will decide to use separate files anyways.

like image 56
Cerad Avatar answered Nov 21 '25 22:11

Cerad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!