Tuesday, 20 August 2013

Dynamically associate external classes to a namespace

Dynamically associate external classes to a namespace

I'm new here. Sorry if I'm not explaining this correctly, it's not very
easy for me to do so.
I'm trying to associate a class to a namespace dynamically, this means:
Suppose file1.php:
// file1.php
namespace myNamespace;
require_once("./file2.php");
// I want to the following dynamically:
use \myClass as myClass;
// So I can be able to do so:
\myNamespace\myClass::myMethod();
...And the file containing the class:
// myClass.php
class myClass {
public function myMethod() {
return "Hello world!";
}
}
What I'm trying to do is load this as following:
\myNamespace\myClass::myMethod();
BUT, the purpose is to do this dynamically. By this I mean to be able to
create a function that does this instead of using require_once (and then
later a use function) function, so file1.php turn into:
// file1.php
namespace myNamespace;
load_comp("myClass");
// And then be able to do:
\myNamespace\myClass::myMethod();
Is there any mode to do so? I want to do this so the file that defines the
namespace doesn't get too big (since I want to do this with more classes),
and to make accesibility easier.
I know how to do this with myClass.php having it's own namespace, but it
becomes more messed up, since it would become something like:
\myNamespace\myClassNamespace\myClass::myMethod();
And then, you would have the namespace \myNamespace\myClassNamespace only
used by myClass (so it's getting more complicated).
Thanks!

No comments:

Post a Comment