Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Docs Classloader

Frank Kleine edited this page Apr 7, 2012 · 1 revision

Table of Contents

The Stubbles class loader

In Stubbles, classes are organized in packages (like Java), which are represented by directories. When ever loading a class from a Stubbles package, we advise you to use the stubClassLoader class instead of require or include. This has several advantages:

  • The class loader knows, where to find the classes, regardless of the include_path setting
  • The class loader remembers, which classes already had been loaded and does not load or search for a class twice
  • All Stubbles classes use the Stubbles class loader and by using it in your own files, you prevent conflicts

Usage

The only time, you should use the require statement is when loading the bootsrtrap class:

#php
<?php
require 'path/to/bootstrap.php'; // contains class stubBootstrap
stubBootstrap::init(array('project' => $path_to_project)));
?>

From now own, use the load() method of the stubClassLoader class:

#php
<?php
stubClassLoader::load('net::stubbles::reflection::stubReflectionClass');
?>

You may also pass more than one class to this method:

#php
<?php
stubClassLoader::load('net::stubbles::reflection::stubReflectionClass',
                      'net::stubbles::xml::stubDomStreamWriter'
);
?>

Most of the Stubbles packages provide one file that you can use to bootstrap all of the important classes of the package:

#php
<?php
stubClassLoader::load('net::stubbles::event::event');
?>

Advanced Features

XJConf and automatic class loading

If you want to use Stubbles classes via XJConf, you may use the net::stubbles::util::xjconf::stubXJConfLoader.

Class Registry

The stubClassLoader can be used as a class registry as well. It knows the non- and the full qualified class name of every class that was loaded via the stubClassLoader.

#php
<?php
// displays 'MyExampleClass'
echo stubClassLoader::getNonQualifiedClassName('example::MyExampleClass');

// displays 'null'
var_dump(stubClassLoader::getFullQualifiedClassName('MyExampleClass'));
stubClassLoader::load('example::MyExampleClass');
// displays 'example::MyExampleClass (length=22)'
var_dump(stubClassLoader::getFullQualifiedClassName('MyExampleClass'));
?>

It is possible to translate full qualified class names into non qualified class names at every time. However it is not possible to translate a non qualified class name into a full qualified class name until the class has been loaded via the load() method.

Static initializing

If the class to load contains a <u>static() method the stubClassLoader will execute this method right after loading the class. The method signature has to be public static function </u>static(), else this will fail or result in undefined behaviour. Because stubClassLoader takes care that every class is only loaded once this method will only be executed once, at the first loading of the class.

Assume the following class:

#php
<?php
class WithStatic
{
    private static $called = 0;
    
    public static function __static()
    {
        self::$called++;
    }
    
    public static function getCalled()
    {
        echo 'WithStatic::__static() has been called ' . self::$called . ' times.';
    }
}
?>

Now we load the class:

#php
<?php
stubClassLoader::load('example::WithStatic');
stubClassLoader::load('example::WithStatic'); // load a second time to demonstrate the behaviour
WithStatic::getCalled();
?>

The result will be:

WithStatic::__static() has been called 1 times.
Clone this wiki locally