[thelist] how object oriented is php4

Tobyn Baugher toby at rsux.com
Thu Mar 27 01:25:39 CST 2003


On Wednesday, March 26, 2003 7:31 PM -0800 Prachi Soni 
<prachi_soni at yahoo.com> wrote:

> I was wondering how object-oriented is php4? What characteristics
> makes it object oriented and what all stops it?.It would be great if
> I get few thoughts on it.

Sadly, OO isn't a first class citizen in PHP4. The performance overhead 
of using it can be noticeable, so I'd say don't overdo it. I manage to 
avoid it altogether happily in decent sized apps by following a sane 
function naming convention. Of course, when compared with other things 
you're probably doing (like accessing a database) the slowdown is 
pretty much negligible, so if you wanna use it you can probably go 
ahead.

As far as the features it has, it has the following:

classes
single inheritance
instance variables
methods
constructors

The features that I notice missing from it vs., say Python or Java:

interfaces (Java)/multiple inheritance (Python)
class variables
differentiation between class and object methods

I know there are ones that I missed, but I miss these every day.

There are other things that people might associate with OO that I don't 
consider strictly OO, but that PHP is missing. The most important one 
of these is exceptions (try/catch/finally). Andrew mentioned function 
overloading.

You can get around function overloading with types, if you *really* 
need to, to an extent by using something like:

<?php
function do_blah($var) {
        if (is_float($var)) { _do_blah_float($var); }
        elseif (is_int($var)) { _do_blah_int($var); }
        else {
                // handle default case...
        }
}

function _do_blah_float($var) {
	// handle floats...
}

function _do_blah_int($var) {
	// handle ints...
}
?>

Of course, with PHP not being a strongly-typed language in the first 
place I've never really found occasion to want to use this. As far as 
overloading the number of arguments, PHP has facilities for dealing 
with variable numbers of arguments to functions. These can be combined 
to get almost any type of overloading you need, though it's nowhere 
near as syntactically clean as a language designed to do it out of the 
box.

Oh, the forthcoming Zend2 engine (that'll come with PHP5, I think) 
promises to be much more OO-friendly among other things. It's looking 
really good. I can't wait to get my hands on it when it's beyond alpha 
quality.

-- 
Tobyn Baugher <toby at rsux.com>
http://www.rsux.com
aim: dieplzkthxbye  icq: 14281524  efnet: toby


More information about the thelist mailing list