1

I have a few PHP classes which are defined like this:

module1Model.php

namespace App\Modules\module1\models;

class module1Model
{
    //... methods etc.
}

module2Model.php

namespace App\Modules\module2\models;

class module2Model
{
    //... methods etc.
}

In some other class i want to use those namespaces. So i placed the following lines at the top of my page:

someClass.php

namespace App\Modules\someClass\controllers;

use App\Modules\module1\models; // Works
use App\Modules\module2\models; // Fails

use Core\Logger\TextLogger; // Works

class somceClass
{
    //... methods etc.
}

So in someClass i'm trying to use both module1 and module2 models. Which are technically not in the same namespace. Just partially. But why is it giving me an error about it then..??

And what would be a good way to solve this problem? I know i can add this to the second line:

use App\Modules\module2\models as module2; // Now works

But i don't really like to do it that way, since i didn't do it like that in all my other classes (using the as keyword). That feels so inconsistent.

Is there any other way to make this work..? Or am i really forced to use the as keyword in all my use declarations...? :-(

1
  • 3
    Nope. The whole purpose of namespaces is to distinguis classes with the same name. Commented Sep 29, 2012 at 20:12

1 Answer 1

1

I guess you want to use the model classes, not their whole namespace. If so, then the following just works:

use App\Modules\module1\models\module1Model;
use App\Modules\module2\models\module2Model;

Example:

namespace App\Modules\module1\models {
    class module1Model
    {
        //... methods etc.
    }
}

namespace App\Modules\module2\models {
    class module2Model
    {
        //... methods etc.
    }
}
namespace App\Modules\someClass\controllers {

use App\Modules\module1\models\module1Model;
use App\Modules\module2\models\module2Model;

use Core\Logger\TextLogger;

    class somceClass
    {
        //... methods etc.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.