Location: PHPKode > scripts > Klass > example.php
<?php

require_once 'Klass.php';

class Person extends Klass
{
	/**
	 * @Accessors
	 */
	protected $_name;
	
	public function __construct($name = null)
	{
		// don't forget to call the parent constructor
		// if not automatic dependency injection will not work
		parent::__construct();
		
		$this->setName($name);
	}
}

class Employee extends Person
{
	/**
	 * @Accessors
	 * @Inject
	 * @var Boss
	 */
	protected $_boss;
	
	/**
	 * @Accessors
	 * @var boolean
	 */
	protected $_trainee = false;
}

/**
 * @Dependency
 */
class Boss extends Employee 
{
	
}

class Freelance
{
	/**
	 * @Inject(Boss)
	 */
	public $boss;
}

// objects extending class, so no need to use Klass::create()
$boss = new Boss('Peter');
$employee = new Employee('Jack');
$employee->setTrainee(true);

// object extending nothing, so we use Klass::create() to automatically inject dependencies
$freelance = Klass::create('Freelance');

// we can also inject dependencies using Klass::inject()
$freelance2 = new Freelance();
Klass::inject($freelance2);

// small tests
printf("Employee is trainee = %b\n", $employee->isTrainee());
printf("Boss is trainee = %b\n", $boss->isTrainee());
printf("Employee's boss name = %s\n", $employee->getBoss()->getName());
printf("Freelancer's boss name = %s\n", $freelance->boss->getName());
Return current item: Klass