My first area of interest in the new Drupal 8 system is to compare how the database abstraction layer compares to that in Drupal 7. The first thing that stood out to me was that there were these “use namespace…” statements at the top of the core/database.inc file that were not present
An Object By Any Other Name(space)
While many of Drupal’s legacy modules use the “hook” methodology as a pre-OO PHP hack to achieve sub-classing, the Database abstraction layer in Drupal 7 was already OOP/Class-based, with the object “DatabaseConnection”. So, we might find out that there is little difference in the actual object plumbing in Drupal 8. Before I could explore the object code, however, locating and understanding the way that Drupal 8 includes code files DOES require some new thinking. Drupal 8 uses the post PHP5.3 “namespace”, something that did not appear in pre-8 versions (confirm this by doing “fgrep “namespace ” ./* -R” inside the Drupal 7 directory). Name spaces, it appears to me, help to avoid collisions with object class names and functions (if nothing else?). The namespace comparmentalizes code according to a “path”, such as “Drupal\Core\Database”, that while mirroring the “physical” directory path, doesn’t have to. Also note how these paths use DOS style backslashes “\” – how hipster-retro-ironic-#back2thefuture is that?.
Namespaces in Drupal 8 Data Module
In the core file “database.inc”, the following 3 namespaces are loaded right off the bat:
I believe that one advantage (temptation? peril?) of namespaces is that they *could* be located in different paths on a disk but occupy the same namespace. So, if one were to write some extension to the Core Database routines, or even something that built off of them, they could locate them in that namespace (such as “use Drupal\Core\Database\Query\Condition;”) and then not only acess the objects, variables and methods in that namespace, but ALSO automatically expose their own objects, variables and methods into said namespace.
With great power comes great responsibility.
More Information on Namespaces in PHP & Drupal
- Namespaces in General & Drupal: http://www.metaltoad.com/blog/improving-our-use-namespaces
- The genesis of namespaces in Drupal 8 (as it happened): https://drupal.org/node/867772
- PHP Namespaces Overview: http://www.php.net/manual/en/language.namespaces.basics.php
- Importing PHP Namespaces: http://www.php.net/manual/en/language.namespaces.importing.php