Aspen:Extending System Classes

From Aspen Documentation

Aspen provides a way for you to extend system classes. For example, if you want to extend the Settings class with your own changes or additions, you'll want to keep those changes separate from the original class so that upgrades will not overwrite anything.

In this example, we'll create an extension to the setting class.

You can store your class files anywhere you like, but for this example we'll store it within the Index module, for demonstration purposes.

First, we need to create a file in modules/Index/mysettings/Mysettings.php.

<?php 
 
class Mysettings extends Settings {
 
	public function __construct(){ parent::__construct(); }
 
	public function testMe(){
		print 'Mysettings works!<br>';
	}
 
}
?>

Then, we need to activate this class in our configuration file. You may user either the config.php are the app.default.config.php files.

$config['custom_classes'][] = array(
				'classname' => 'Mysettings', 			
                               'root'=>MODULES_PATH.DIRECTORY_SEPARATOR.'Index',
				'extends' => 'Settings');

The root index of the array passes the root file system path the library can use to locate the file.

The extends key indicates the name of the original class that this child class will extend.

After activation and loading, you may access the new child and parent through the original member variable of the original class.

For example, to access the testMe function from our child class, we could do so by:

$this->APP->settings->testMe();