2. Writing Classes

We turn writing classes in Eden into an exciting experience. There is basically one requirement to integrate your class and to begin harnessing the power of Eden.

  1. Extend your class with Eden_Class
Figure 1. Defining a Class in PHP 5.3.x
class My_Session extends Eden_Class {
	const INSTANCE = 1;	

The above example shows how the requirements would look like in our new class. The constant INSTANCE tells Eden to instantiate this class for the first time and next time, return that same instance. For multiple instances you can just remove that constant. We can then instantiate our new class shown in Figure 2.

Figure 2. Instantiating our class
echo eden()->My_Session(); //--> My_Session

When we instantiate our class we now have the option of using eden() like our other examples. You can also use new My_Session or My_Session::i() to instantiate this class. Passing arguments to a constructor can be achieved like in Figure 3.

Figure 3. Passing arguments to the constructor
class My_Session extends Eden_Class {
	public function __construct($name, $age) {}

echo eden()->My_Session('Chris', 29); //--> My_Session

Now that we have a class, let's build a method called start() which demonstrates calling other methods in Eden.

Figure 4. The power of $this
class My_Session extends Eden_Class {
	public static function i() {
		return self::_getSingleton(__CLASS__);
	public function start() {
		echo 'Starting Session ...';
		//get the session id
		$session = $this->Eden_Session()->start()->getId();
		//store session id in cookie
		$this->Eden_Cookie()->set('session_id', $session);
		return $this;

eden()->My_Session()->start(); //--> Starting Session ...

So we made our start() echo a string, start a PHP session and store the session ID in a cookie. You'll notice in our start method that we can use $this instead of eden() to call any other class extended with Eden_Class.

It's good practice, when defining our methods to return $this instead of nothing. This allows our class to call other methods or classes without leaving the chain (chainability).

Extending Classes

All classes in Eden can be extended naturally with PHP. In our My_Session example, it would probably be better to extend it with Eden_Session to inherit the rest of Eden_Session's methods. Figure 5 shows us us how we can do that.

Figure 5. Extending a class
class My_Session extends Eden_Session {
	public function start() {
		echo 'Starting Session ...
'; //get the session id $session = parent::start()->getId(); //store session id in cookie $this->Eden_Cookie()->set('session_id', $session); return $this; } } echo eden() ->My_Session() ->start() //--> Starting Session ... ->getId(); //--> [SOME SESSION ID]

So great, now My_Session becomes more of a useful class with the help of $this, chainability and extending. A common problem is that, previously defined code calling Eden_Session will still be calling Eden_Session. There maybe a case we may want all code to use My_Session instead.

Scenario 1
We would like to make a plugin for other developers using Eden that sends an email everytime a new session is started. In other developers code, they may already be using Eden_Session. We would either need to rewrite Eden_Session for everyone that uses your plugin (which is a no no..)
or tell them to change their code to use My_Session instead.

Scenario 2
You would like to modify a plugin that uses Eden_Session to use My_Session instead.

In both scenarios it's bad practice to directly modify someone else's code, especially if it's regularly updated because there's a likely chance your changes will conflict with theirs. This is a more common problem with all PHP frameworks, CMS', plugins etc., but with Eden as the core of our application, the problem becomes trivial. The following example shows how we can solve both scenarios.

Figure 6. Solution: Back to the Router
//Everytime Eden_Session is called, call My_Session instead
eden('route')->getClass()->route('Eden_Session', 'My_Session');

//Example of calling Eden_Session
eden()->Eden_Session()->start(); //--> Starting Session ...

eden('session')->start() //--> Starting Session ...

Eden_Session::i()->start(); //--> Starting Session ...


It's not too difficult to start programming in Eden. If you start following our style of programming you'll notice your code to be more readable overall because we can eliminate the need of ::, new and reduce the amount of variables you consume by chaining. In our next sections we will be talking about more intermediate subjects and complete features in detail. Although experimenting with writing classes in Eden will entertain you for sometime, head over to 3. Autoloading whenever your ready!

© 2012 Openovate Labs. All rights reserved.