質問

I've got a few PHP5.2- frameworks using long classnames like such:

class Product_Sub_SomeClass {
    ...
}

I'd like to be able to provide a way for PHP5.3+ users to use namespaces and short class names whilst retaining PHP5.2 compatibility of the framework.

The above example class should be available in PHP5.3 developers' code as:

use Product\Sub;
$someclass = new SomeClass();

But the exact same class (same sourcecode) should also be usable to PHP5.2 users as:

$someclass = new Product_Sub_SomeClass();

Can I just provide an additional file that handles all the aliasses or is there some other or better mechanism available?

What is best practice to provide namespaces and class aliasses to PHP5.3+ users of a PHP5.2- framework?

役に立ちましたか?

解決

Found my own answer to this problem:

class Product_Sub_SomeClass {
    ...
}

// Alternative PHP 5.3+ namespace\class
if (function_exists('class_alias')) {
    class_alias('Product_Sub_SomeClass', 'Product\Sub\SomeClass');
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top