Configure a namespace but instantiate without it

19 views
Skip to first unread message

Lawrence

unread,
May 24, 2021, 9:37:43 AM5/24/21
to composer-users
It's possible to configure a namespace for my package but not having to define it when instantiante the object?

eg: I want to create a namespace like MyPackages\MyClass but when I need to instantiate the class I want to write just new MyClass, and not new MyPackages\MyClass.

Reason: I have a class that some people use, and I want to update the class with a namespace to be autoloaded, but I don't want to break the code that is already using the class.

Jordi Boggiano

unread,
May 24, 2021, 10:16:02 AM5/24/21
to compose...@googlegroups.com

You can move your class to a namespace, and in the class definition file add class_alias('MyPackage\MyClass', 'MyClass');

That's a decent way to provide BC IMO.

See https://www.php.net/class_alias

--
You received this message because you are subscribed to the Google Groups "composer-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to composer-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/composer-users/1573f340-cd8f-4263-bb7d-80a31fc0e962n%40googlegroups.com.
-- 
Jordi Boggiano
@seldaek - https://seld.be
Reply all
Reply to author
Forward
0 new messages