Creating Objects Dynamically

This documentation covers functionality of objects that use a class that is extended from WWW_Factory class. Methods and calls in this documentation can be used when building your Models, Views and Controller classes and their functionality.

Introduction

Wave Framework is built around the concept that every object of the system is loaded dynamically. This is done through Factory and every object in a system built on top of Wave Framework benefits from being extended from that Factory since the Factory gives additional functionality to each of those objects.

Of course it is also possible to include Models, Views and Controllers and other objects that are not extended from Factory, so both options are there.

Dynamic loading of objects is useful for one big reason: this means that your system only loads the resources and classes that it actually needs and actually uses. This lowers the amount of memory your system takes during a request as well as increases the processing speed of your requests.

Loading Libraries

Function libraries should never be declared global and accessible all over the software, since if most of your software never uses these functions, then such a functionality only adds weight to your software and how quickly it can respond to requests.

Factory class allows you to easily load libraries dynamically. Dynamically loaded libraries need to follow these rules:

You can then load libraries like this:

	
	$model=$this->loadLibrary('example');
	

You can also dynamically call one of the functions from that library in the same call and return results from that call:

	
	$model=$this->loadLibrary('example','myfunction');
	

Note that the callback function does not have to be from within the library itself, it can be a function that is already defined elsewhere previously.

There is also an option to load JavaScript script and library files as well as CSS stylesheets from within Views (but also allowed from Models and Controllers, though this is not recommended). This means that you can tell within a View to load a specific script file and View Controller will take this into account when building the headers. To do this you can call this:

	
	// This adds a CSS stylesheet to the request headers (from '/resources/styles/' folder)
	$this->viewHeader('my-style.css','style');
	// This adds a JavaScript file to the request headers (from '/resources/scripts/' folder)
	$this->viewHeader('my-script.js','script');
	// This adds a JavaScript library file to the request headers (from '/resources/libraries/' folder)
	$this->viewHeader('my-library.js','library');
	// This adds the entire string in the end right between HTML head tags
	$this->viewHeader('<meta name="test" value="Lorem Ipsum"/>');
	

Loading Objects

Wave Framework groups classes in four: Models, Views, Controllers and Objects. You can read more about the purpose of each of those classes in MVC Pattern documentation page. But in order for these objects to be loaded dynamically, there are a couple of rules that have to be followed.

Models

Dynamically loaded models need to follow these rules:

In order to load a model dynamically, you have to call getModel() method, like this:

	
	$model=$this->getModel('example');
	

The above code would load the class from '/models/model.example.php' and then create the object from class that is named 'WWW_model_example'. After this the model can be used in any way you would otherwise use an object, to call methods and so on and so forth.

It is also possible to dynamically load a class, create an object, and call a method of that object with data all in a single method call. For example, this would do the same thing as the previous example, except it would also call a method called 'test()' with input data sent to that method:

	
	$input='some-input-string';
	$result=$this->getModel('example','test',$input);
	

After this call, the above example's $result variable would contain the result from that method and not the object itself. This is useful in some cases, where you know that you only need to use an object once.

Views

Dynamically loaded views need to follow these rules:

In order to load the view dynamically, you have to call getView() method, like this:

	
	$view=$this->getView('example');
	

The above code would load the class from '/views/view.example.php' and then create the object from class that is named 'WWW_view_example'. After this the view can be used in any way you would otherwise use an object, to call methods and so on and so forth.

It is also possible to dynamically load a class and execute a method in that class. This is done similarly to Models and you should refer to examples about Models as shown above to see how that is done.

Controllers

Dynamically loaded controllers need to follow these rules:

In order to load the controller dynamically, you have to call getController() method, like this:

	
	$controller=$this->getController('example');
	

The above code would load the class from '/controllers/controller.example.php' and then create the object from class that is named 'WWW_controller_example'. After this the controller can be used in any way you would otherwise use an object, to call methods and so on and so forth.

It is also possible to dynamically load a class and execute a method in that class. This is done similarly to Models and you should refer to examples about Models as shown above to see how that is done.

Other Objects

It is also possible to load other classes dynamically in Wave Framework. While there are some restrictions to loading these classes dynamically - for example you cannot set construction variables - sometimes it is useful when the loaded class serves occasional purpose.

Dynamically loaded objects need to follow these rules:

In order to load the object dynamically, you have to call getObject() method, like this:

	
	$view=$this->getObject('example');
	

The above code would load the class from '/resources/classes/class.example.php' and then create the object from class that is named 'example'. After this the object can be used in any way you would otherwise use an object, to call methods and so on and so forth.

It is also possible to dynamically load a class and execute a method in that class. This is done similarly to Models and you should refer to examples about Models as shown above to see how that is done.

Construction Methods

PHP has a magic method called '__construct()' that is called whenever an object is created. This can be used to load additional functionality to an object when it is created, like create some Database connections or initialize configuration.

In Wave Framework, if your object is created from a class that is extended from WWW_Factory, then you cannot define a '__construct()' method for that object as it is not possible to overwrite the method from WWW_Factory. Instead you should define '__initialize()' method instead, as Factory's own construction will also make a call for that method, if it is defined.

JavaScript Factory

Wave Framework also includes a JavaScript Factory that is more limited, than PHP Factory. But it still allows you to dynamically load Objects as well as Libraries. This is done exactly the same way. For example:

	
	// Create a new Factory class
	var Factory=new WWW_Factory();
	// Get new instance of the 'example' class
	var myExample=Factory.getObject('example');
	// And now you can use that object like any other
	myExample.doSomething();
	

And you can also load libraries:

	
	// Factory class is required
	var Factory=new WWW_Factory();
	// Load the library
	Factory.loadLibrary('example');
	// Use some function from that library
	doSomething();