src/Core/Extensible.php
public static function add_extension($classOrExtension, $extension = null)
    {
        if ($extension) {
            $class = $classOrExtension;
        } else {
            $class = get_called_class();
            $extension = $classOrExtension;
        }

        if (!preg_match('/^([^(]*)/', $extension, $matches)) {
            return false;
        }
        $extensionClass = $matches[1];
        if (!class_exists($extensionClass)) {
            user_error(
                sprintf('Object::add_extension() - Can\'t find extension class for "%s"', $extensionClass),
                E_USER_ERROR
            );
        }

        if (!is_subclass_of($extensionClass, 'SilverStripe\\Core\\Extension')) {
            user_error(
                sprintf('Object::add_extension() - Extension "%s" is not a subclass of Extension', $extensionClass),
                E_USER_ERROR
            );
        }

        // unset some caches
        $subclasses = ClassInfo::subclassesFor($class);
        $subclasses[] = $class;

        if ($subclasses) {
            foreach ($subclasses as $subclass) {
                unset(self::$extra_methods[$subclass]);
            }
        }

        Config::modify()
            ->merge($class, 'extensions', array(
                $extension
            ));

        Injector::inst()->unregisterNamedObject($class);
        return true;
    }
public static function add_extension($classOrExtension, $extension = null)
    {
        if ($extension) {
            $class = $classOrExtension;
        } else {
            $class = get_called_class();
            $extension = $classOrExtension;
        }

        if (!preg_match('/^([^(]*)/', $extension, $matches)) {
            return false;
        }
        $extensionClass = $matches[1];
        if (!class_exists($extensionClass)) {
            user_error(
                sprintf('Object::add_extension() - Can\'t find extension class for "%s"', $extensionClass),
                E_USER_ERROR
            );
        }

        if (!is_subclass_of($extensionClass, 'SilverStripe\\Core\\Extension')) {
            user_error(
                sprintf('Object::add_extension() - Extension "%s" is not a subclass of Extension', $extensionClass),
                E_USER_ERROR
            );
        }

        // unset some caches
        $subclasses = ClassInfo::subclassesFor($class);
        $subclasses[] = $class;

        if ($subclasses) {
            foreach ($subclasses as $subclass) {
                unset(self::$extra_methods[$subclass]);
            }
        }

        Config::modify()
            ->merge($class, 'extensions', array(
                $extension
            ));

        Injector::inst()->unregisterNamedObject($class);
        return true;
    }
public static function add_extension ( $ classOrExtension , $ extension = null ) { if ( $ extension ) { $ class = $ classOrExtension ; } else { $ class = get_called_class ( ) ; $ extension = $ classOrExtension ; } if ( ! preg_match ( '/^([^(]*)/' , $ extension , $ matches ) ) { return false ; } $ extensionClass = $ matches [ 1 ] ; if ( ! class_exists ( $ extensionClass ) ) { user_error ( sprintf ( 'Object::add_extension() - Can\'t find extension class for "%s"' , $ extensionClass ) , E_USER_ERROR ) ; } if ( ! is_subclass_of ( $ extensionClass , 'SilverStripe\\Core\\Extension' ) ) { user_error ( sprintf ( 'Object::add_extension() - Extension "%s" is not a subclass of Extension' , $ extensionClass ) , E_USER_ERROR ) ; } // unset some caches $ subclasses = ClassInfo :: subclassesFor ( $ class ) ; $ subclasses [ ] = $ class ; if ( $ subclasses ) { foreach ( $ subclasses as $ subclass ) { unset ( self :: $ extra_methods [ $ subclass ] ) ; } } Config :: modify ( ) -> merge ( $ class , 'extensions' , array ( $ extension ) ) ; Injector :: inst ( ) -> unregisterNamedObject ( $ class ) ; return true ; }
Add an extension to a specific class.

The preferred method for adding extensions is through YAML config,
since it avoids autoloading the class, and is easier to override in
more specific configurations.

As an alternative, extensions can be added to a specific class
directly in the {@link Object::$extensions} array.
See {@link SiteTree::$extensions} for examples.
Keep in mind that the extension will only be applied to new
instances, not existing ones (including all instances created through {@link singleton()}).

@see http://doc.silverstripe.org/framework/en/trunk/reference/dataextension
@param string $classOrExtension Class that should be extended - has to be a subclass of {@link Object}
@param string $extension Subclass of {@link Extension} with optional parameters
as a string, e.g. "Versioned" or "Translatable('Param')"
@return bool Flag if the extension was added
Add an extension to a specific class .