This factory supports creation of one of each of the kinds of things one might require in any Drupal project/module, except for render objects, handled by IRenderFactory, and XHTML tags, handled by ITagFactory.
Because this factory has knowledge of the module machine name, the signatures of the creation methods are more concise than the matching constructors of the classes for some products (do not have to include the module machine name as a parameter). This is one very simple example of the value-adding a factory can offer over direct construction.
In general, factories enable one to apply policies and diagnostics across all created products in a centralised place. (This can be particularly important when one is creating persistent database entities).
Creating classes directly is discouraged. Instead, use a factory function that creates the appropriate object and returns it. This provides two benefits:
- It provides a layer of indirection, as the function may be written to return a different object (with the same interface) in different circumstances as appropriate.
- PHP does not allow class constructors to be chained, but does allow the return value from a function or method to be chained.
© 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.