From Drupal: namespaces:
Modules creating classes should place their code inside a custom namespace. The convention for those namespaces isnamespace Drupal\example_module
To support autoloading, these classes should be placed inside the correct folder:sites/all/modules/example_module/lib/Drupal/example_module <module folder> /lib/ <namespace>
But as remarked at A little confused w/ Namespaces & AutoLoading:
The last paragraph ... conflicts w/ the examples of modules declaring their classes in the .info file.
How are we supposed to store class files using namespaces, and the advantages of autoload?
For example, Commerce declares in commerce_product.info:files = includes/views/handlers/commerce_product_handler_area_empty_text.inc
seemingly creating class files arbitrarily in the includes folder.
Applies only to D8
Posted by mikeejt on March 8, 2013 at 10:15am
After doing some research, found that the last paragraph above only applies to Drupal 8.
According to this comment by Crell: http://drupal.org/node/1254488#comment-4885794 Drupal's autoloader in D7 will be replaced by Symfony's autoloader, which uses php 5.3's PSR-0 autoloader.
As Drupal modernizes more to OOP, the use of namespaces will help avoid class name clashes; but to take advantage of Symfony's autoloader, class files need to be created in the correct structure.
Hat tip to http://rbayliss.net/story/understanding-drupal-8-autoloading for helping me understand Drupal's (D7 & D8) autoloader works.
© Copyright 2001 - 2016 Webel IT Australia (ABN: 67 677 268 579). All rights reserved (except as specified below).
PHP code examples from Webel IT Australia on this site are distributed under the GNU General Public License.
Excludes text and code snippets from Drupal.org quoted for educational purposes.
Drupal’s online documentation is © 2000-2014 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0.
PHP code from Drupal.org is distributed under the GNU General Public License.
Drupal® is a registered trademark of Dries Buytaert.
Text quoted from Wikipedia for educational purposes is made available under the Creative Commons Attribution-ShareAlike License.
Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
Site developed by Webel IT Australia.