Categories


Archives


Recent Posts


Categories


Interfaces and Abstract Classes

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Interfaces and abstract classes are one of those next tier topics that will initially flummox someone whose background has been primarily linear programming.

At least, that’s how I felt three to four years ago while trying to understand the changes coming to PHP 5. The basics were easy enough; “OK, so if my class implements an interface or extends an abstract class there’s a bunch of methods it must have”. Why anyone would use these was beyond me until I started working with PHP 5 on a regular basis.

The missing piece for me was references. I knew that PHP 5’s object model had changed so that objects were passed by reference. This was a giant pain in the ass for anyone trying to convert a PHP 4 application over to PHP 5. It was also the most significant change made to PHP.

I said the object model had changed such that objects were passed into functions by reference. While technically accurate, this description is limited. What PHP 5 really did was to start treating objects as a reference point to a particular block of memory. This is how objects behave in Java, and many other languages.

In plain english, that means whenever you assign an object to something, you’re always referencing the same object. Passing by reference is a side effect of this core concept.

$a = new StdClass();
$b = $a;
$b->message = "You'll never see me";
$a->message = "hello object\n";

echo $a->message; 
echo $b->message;

//output
hello object
hello object

So, what does this have to do with interfaces and abstract classes? Given that objects are references, objects end up being passed around in methods and operated on a lot

...
$foo = new Bar();
$this->DoStuffToIt($foo);
 ....
private function DoStuffToIt($foo){
     ...
     $foo->SetThis();
     $foo->SetThat();
     $foo->AnotherMethodThatChangesObjectState();
 }

It’s also a tenant of OOP that methods which accept objects don’t care what kind of object they’re passed. Methods are blindly called irrespective of what type the object actually is. The Bar object above could be replaced with a Baz object, or any object, so long as it shared the same methods.

This is a big reason why interfaces and abstract classes are important. They allow a systems programmer to say

“OK, so you’re going to write some new object and pass it into my
methods, eh? Fine, just make sure your object can respond to these
methods or bad things will happen. If fact, I’m not even going to
let my system talk to your object before it proves it can respond
to the these methods

This also underscores a higher concept change that went on in PHP between versions. PHP 4 was primarily about building applications, using “the web” as a system. PHP 5 was (intentional or not) an attempt at building a language for developing stand-alone systems in and of themselves.

Originally published January 1, 2009

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 1st January 2009

email hidden; JavaScript is required