PHP5 and Questioning OOP
An Interview with Sterling Hughes on PHP5 from the PHP-Con site:
Personally, while I have programmed with both C++ and Java, I am mostly a procedural guy. I feel that most object oriented programming consists of abstracting different problems into the same problem and then resolving that problem. Sure it makes it easier, but its incredibly inefficient for developing a small set of interconnected programs, which really is the whole point in web development.
That’s not to say I’m unhappy with PHP5. Most of the stuff in PHP5 is great, most notably exceptions; I’ll certainly be taking advantage of many of the new features in Zend Engine 2. But most of my code is procedural, and Zend Engine 2 is mostly an object oriented functionality upgrade.
That’s the second time this week I’ve come across an opinion that questions the benefits of the OOP-at-all-costs approach to programming. The first was in Paul Graham’s The Hundred Year Language:
Somehow the idea of reusability got attached to object-oriented programming in the 1980s, and no amount of evidence to the contrary seems to be able to shake it free. But although some object-oriented software is reusable, what makes it reusable is its bottom-upness, not its object-orientedness. Consider libraries: they’re reusable because they’re language, whether they’re written in an object-oriented style or not.
I don’t predict the demise of object-oriented programming, by the way. Though I don’t think it has much to offer good programmers, except in certain specialized domains, it is irresistible to large organizations. Object-oriented programming offers a sustainable way to write spaghetti code. It lets you accrete programs as a series of patches. Large organizations always tend to develop software this way, and I expect this to be as true in a hundred years as it is today.
As undergraduates, we have been exposed to the joys of object oriented programming from our first week at Uni, possible because we’re supposedly destined for the large organisations Paul mentions. It’s nice to see the view from the other side of the fence for a change.
Martin - 11th April 2003 12:35 - #
Nicholas Wolverson - 11th April 2003 16:58 - #
Lach - 12th April 2003 01:53 - #
Tim Parkin - 12th April 2003 13:23 - #
Lach - 13th April 2003 04:51 - #
Tim Parkin - 13th April 2003 22:06 - #
Object oriented programming really is a matter of personal taste, if you ask me; however, I find it much easier to reuse code and maintain old code that has been constructed in an object oriented manner.
The idea of "reusing" code does not simply mean accessing the same tired library from different applications, but it also includes what is done when you extend a class to provide additional, modified, or specific functionality over its parent class.
If I define an abstract "Database" class, I can build a framework around this generic "Database" class without even ever having a real, implemented database subclass. Others can then create their own specific implementations for whatever database they care about and it will work in my framework. Additionally, if one was to distinguish that all forms of databases to be implemented would fall into either the "server" database or "file" database categories, many of the more specific operations could be outlined in advanced as well, such as opening a connection, or opening a file.
try
{
$DB = new MySQLDatabase("localhost","root","");
$siteTitle = $DB->Query("Select SiteTitle From Options")->GetNext()['SiteTitle'];
catch(DatabaseException $e)
{
# ...
}
A clear benefit is that this structure will not only catch exceptions of type DatabaseException, but all subclasses of that exception type, while still allowing for the developer to catch a more specific exception type first.
Going one step further with the "framework" idea I talked about. Lets imagine you are helping to develop the newest and coolest web based forum system in PHP and your team is hoping to support some sort of database abstraction system. Using a system such as this, you can code your functions without having every target database library accessible to you:
function InstallForum(Database $DB)
{
$DB->Query("Create Table ...");
}
# ...
function GetThread(Database $DB, $id)
{
$DB->Query("Select * ...");
}
Of course, this introduces concerns of SQL compatability, but keep in mind that these are just examples. I personally would like to see the procedural implementations of such ideas.
OO programming compartmentalizes your code, so you can have 20 different "Close()" functions all doing various things, but since they are differentiated by being defined within separate classes, all you need to remember is one function name.
$Socket->Close(); $File->Close(); $Door->Close();as opposed tosocket_close($socket); fclose($file); door_close($door);One final point I would like to highlight is the general, plain english readability of OO code. It can usually be read fluidly in English.
$Dog->Bark(); as opposed to bark($dog);Andrew Moyer - 7th January 2004 23:09 - #
for ex:
obj_car = new_car(); car_set_color(&obj_car, COLOR_RED);
Having said that, Java offers *far* more OO built-in supports than C, but it CAN be done in C!
Please don't get mixed up between implementation and concept. I don't like Java implementation, but I think the concept is fantastic. Between Java and C, I will definetely choose Java as my programming language because of its great OO support.
This is true for php4 and php5 too, I'm definetely in favour of php5 because of its built-in OO features. You can do it in php4, but php5 offers much better OO support.
sid - 26th April 2005 00:19 - #
William - 21st July 2006 21:01 - #