Introducing PHP Namespaces

Some of you may have seen already this huge commit on GitHub which contains a completely overhauled core of pimcore using real PHP namespaces and lots of other improvements in detail. 

This is definitly one of the biggest changes in the core since ever and we are proud to make the system for you not only looking more modern but also more flexible, structured and comfortable. This is also a big step in terms of interoperability in combination with other libraries and components out there, like the ZF2 and Symfony and of course many others. 

But the most important fact is, that we tried to make this change 100% downward compatible to prior versions of pimcore and I hope we did a good job there. :) But of course we need your feedback and I promise we'll do our best to make the upgrade as easy as possible for you, but we definitly need your help on this topic!

The changes are part of all builds greater than 3303, so just update your development environment to the latest build using the updater in pimcore. If there are problems with your installation please try to fix the problem locally and provide us a patch file or create a pull request on github, we'll review and integrate your contributions as soon as possible. 

So how does this work and what has changed ...

Basically we replaced the prefixed class names by real namespaces so the full name (incl. namespace) of a class stays the same. We've created a custom autoloader which does not only load the classes but also creates alias on-the-fly for the legacy class names. In our opinion this is a very simple and lightweight solution with nearly no overhead. 

Example: Pimcore_Controller_Action_Frontend => Pimcore\Controller\Action\Frontend

The important thing here is that both class names work at the same time, so you can do the following for instance: 

use Pimcore\Model\Object\Service; 
$service = new Service(); 
if($service instanceof \Object_Service) {
    // yes that will be true ;-) 
}

So there should be no need to change existing code and you can further develop your projects using the old style or by using them together at the same time. 

Unfortunately it wasn't possible to do this 1:1 renaming for all of the classes, there are 2 exceptions: abstract classes, interfaces and list classes. The following should give you an overview of what has changed: 

Abstracts: 
Element_Abstract => Element\AbstractElement (in the namespace Pimcore\Model)
Object_Abstract => Object\AbstractObject
Pimcore_API_Plugin_Abstract => Pimcore\API\Plugin\AbstractPlugin
.... 

Interfaces: 
Element_Interface => Element\ElementInterface
Pimcore_API_Plugin_Interface => Pimcore\API\Plugin\PluginInterface

Lists
Document_List => Document\Listing
Object_Product_List => Object\Product\Listing

This matches with the naming conventions of Zend Framework 2 and Symfony, so the idea should be clear. But this was not the reason to change it, we had no other choice since reseved keywords cannot be used as a class name or segment of a namespace. Again, this changes are also automatically handled by the custom autoloader, so you can still use both at the same time => 100% backward compatible. 

Another thing that has changed is that you don't have to use this ugly call Object_Abstract::getById(); anymore, to get an object of an unknown type, now you can simply use Object::getById() as known from documents and assets in the past.

So yes, that's basically what we did the last weeks ;-) 

Of course the code of our demo is already using the namespaces, so you can browse the code to see how it looks like, or download the sample data package (demo version) and try it locally. 

Here are some examples: 
https://github.com/pimcore/pimcore/blob/master/website_demo/controllers/AdvancedController.php
of course you can look at any of them: https://github.com/pimcore/pimcore/tree/master/website_demo/controllers 

Views stay basically the same, but of course it depends on your code. Same for Plugins, they should just work without a modification, but the example plugin is of course also useing namespaces already: 
https://github.com/pimcore/pimcore/tree/master/pimcore/modules/extensionmanager/example-plugin/Example

So the whole codebase of pimcore is already changed, except the unit tests which we will migrate later, but this was necessary to ensure the backward compatibility of course. 

So that's it! We are really very curious what you think and hope that there's a lot of feedback!

 

Looking to gain more Pimcore knowledge? The training platform Pimcore Academy offers on-demand video courses about many Pimcore topics. The content is focused on software developers. It includes code samples, tutorials, and links to further information. Prove your expertise and earn 3 official certificates. Start learning to maximize your Pimcore know-how.

Author:Christian Fasching
Christian Fasching
  • CPO
29 articles by this author

Read more Pimcore news

110 000+ businesses trust Pimcore. Discover our 550+ favorite success stories.