Shiny Red Apples

Shiny Red Apples (Personal Blog of Harald Ponce de Leon)

106 notes &

OSCOM v3.0 Namespace Usage

Namespace What\Is\It?

Namespaces allow scopes to be added to classes and are used to group related classes and to prevent collision of class names. The larger the framework grows and the more components that become available and integrate with the framework, the greater the possibility that a collision of class names can occur.

Prior PHP v5.3 and namespaces, vendors worked around this problem by simulating namespaces with extra long class names. Before we introduced namespaces into our framework, long class names where used such as:

  • OSCOM_Language
  • OSCOM_Site_Admin_Application_Administrators
  • OSCOM_Site_Admin_Language

Implementing namespaces in the framework has allowed us to group classes with the following scopes:

Core Classes
osCommerce\OM

Site Classes
osCommerce\OM\Site\{Site}

Application Classes
osCommerce\OM\Site\{Site}\Application\{Application}

Using Namespaces

Namespaces are declared using the namespace keyword, for example:

File: osCommerce/OM/Site/Admin/Language.php

<?php
  namespace osCommerce\OM\Site\Admin;

  use osCommerce\OM\OSCOM as OSCOM;
  use osCommerce\OM\XML; // the same as "as XML"
  use osCommerce\OM\Registry; // the same as "as Registry"

  class Language extends \osCommerce\OM\Language { }
?>

The Admin Site Language class is defined within the osCommerce\OM\Site\Admin namespace and extends the Core osCommerce\OM\Language class.

Classes can be called by their class name if they reside in the same namespace scope, by their qualified name if they reside in a deeper scope (eg, Subnamespace\ClassName), and by their fully qualified name if they reside in different namespace scope entirely (eg, \Scope\ClassName).

The Admin Site Language class accesses three classes outside of its namespace scope: osCommerce\OM\OSCOM, osCommerce\OM\XML, and osCommerce\OM\Registry. These can be called using their fully qualified class names or can be aliased to shorter names using the use keyword.

It’s A Kind Of Magic

Classes are now automatically loaded without the need to use include() or require(). This is possible by utilizing the PHP 5 autoload feature.

The autoloader utilized treats the namespace scope as a directory structure. This means that using the osCommerce\OM\Site\Admin\Language class will automatically try to include osCommerce/OM/Site/Admin/Language.php if it has not already been included.

This follows the final proposal standard defined by the PHP Standards Working Group for autoloader interoperability between frameworks.

Our final framework structure will consist of two main directories: the framework itself and a public directory containing the publicly accessible files such as product images, template stylesheets, and javascript. This allows the framework directory to reside outside the public html directory to further secure the installation.

Community Feedback

Feedback to this blog entry can be posted on the following topic in the community support forums:

http://forums.oscommerce.com/topic/358932-oscom-v30-framework-optimized-for-php-v53/

Filed under oscom30

  1. haraldpdl posted this