YASB – Yet Another Symfony Blog

January 25, 2007

Getting advanced with objects

Filed under: PHP, Tips and tricks — Krof Drakula @ 10:05 am

Just a quick note – if you’re into strict systems specification and architecture, you’re no doubt no stranger to object-oriented patterns and interfaces. In case you were ever tempted to use interfaces in your application, Symfony has a simple way of declaring an instance and having it autoload into the program stack: just declare your interface in a file, named as the interface itself, plus the suffix interface.php. Simple, no?

UPDATE: Per ruzz’s request, I’m posting a quick example:

Say you’re trying to define a delivery provider in the form of an object, where you could specify parameters like dimensions, mass and whatnot and have the delivery fee be calculated by it. If you’ve ever dealt with different companies, you’re aware that every single delivery service has their own way of billing you for the service, resulting in vastly different calculations from initial parameters.

Now, you could define a base abstract DeliveryProvider object and inherit from that, but you run into some problems when doing that:

  • when trying to make changes to the model, you’re bound by the form of the base object
  • constructors and methods are to be overriden for each of the objects, making the base object more or less just a specifiation for those methods

There is a way of separating inheritance from outer surface – interfaces. Basically it’s just a list of methods and properties that an object must have available as public members, but in the big picture, in ensures that you actually write all the methods specified in the interface.

An example would be to illustrate my point by using a base class A:

// lang php
class A {
  ...
  function doSomething() { ... }
  ...
}

Now, say that we define object B as inherited from A and we forget to redefine a function:

// lang php
class B extends A {
  ...
}

If we forget to declare the function, B::doSomething() would just execute A::doSomething(), which we do not want.

Well, there are 2 ways of dealing with this – either define all the functions in the base class as abstract and let that handle function declarations, or use interfaces, which you could define like this:

// lang php
interface IA {
  public function doSomething();
  public function doSomethingElse($parameter);
}

Implementing an interface is in much the same way as inheriting from a base class:

// lang php
class C implements IA {
  public function doSomething();
}

Now, the last two snippets would cause a fatal error when parsing class C, stating that there’s still 1 undeclared abstract method in C (doSomethingElse()). Now note that the interface also specifies parameters, which are to be declared, along with parameters, which basically makes an interface the basis of an API.

The other good thing about interfaces is that it partially offsets the need to have multiple inheritance, like in C++, since a single class can implement many intefaces. Example:

// lang php
class C implements IA, IB {
  ...
}

There are many interfaces available in PHP to make objects behave in a certain way, and implementing them enables things such as array emulation, iteration, etc. All this in a convenient interface package called the SPL.

Implementing interfaces in Symfony is therefore very easy – just declare an interface in a file whose name is the interface’s, suffixed by .interface.php.

Happy coding!

1 Comment

  1. nice tip.. would be great if you flushed out an implementation example. btw, glad you’re blogging so often, gives me a bit of symfony stuff to read everyday :)

    Comment by ruzz — January 26, 2007 @ 12:29 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress