Skip to content

User Defined Services

Devin Smith edited this page Dec 10, 2015 · 4 revisions

Services in Tipsy are static objects. They are accessible in the same ways as Built in Services. Services can be arrays, closures, classes or class instances.

Array

$tipsy->service('Service', [
	stuff => function() {
		// some stuff
	}
]);

Closure Function

Using a closure allows for the use of Dependency Injection as you can see with this $View example.

$tipsy->service('Service', function($View) {
	$service = [
		stuff => function() use ($View) {
			// some stuff
		}
	];
	return $service;
);

External Class

class Service extends Tipsy\Service {
	public function stuff() {
		// stuff
	}
}
$tipsy->service('Service');

Class Instance

class Service extends Tipsy\Service {
	public function stuff() {
		// stuff
	}
}
$tipsy->service('Service', new Service);

Anonymous Class

$tipsy->service('Service', new class() extends Tipsy\Service {
	public function stuff() {
	}
});