Re: Are PHP5 features worth it?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



# zorglub_olsen@xxxxxxxxxxx / 2006-12-19 19:05:23 +0100:
> I'm writing a PHP program and I've got a number of concerns. The program is
> about 20 KLOC at the moment, but will probably grow quite a lot.
> 
> I'm using OOP throughout, and I don't really have a lot of problems with
> what PHP4 can do with OOP. PHP5 does have a nice feature or two, eventhough
> none seems essential to my particular style of programming. I don't mind
> using what PHP5 offers where it makes sense, but where's that? Exceptions
> and new OOP features?
> 
> Exceptions: I just don't see the big advantage. I've got errors and messages
> bubbling up through object layers as it is, and exchanging that with a
> wholly new structure seems more trouble that it's worth. I've read several
> books on how cool PHP5 is, but the arguments for using exceptions seem to
> boil down to "Java has them". Nowhere have I seen good examples that really
> show how well exceptions solve real problems, all examples seem to show are
> that 5 lines of try/catch are somehow sexier than 5 lines of if/else.

One of the differences is that the if/else lines need to be in all layers
between where the error might happen and where it will be ultimately
handled. With exceptions, the try/catch can be detached (as long as it's
the right thing to do, of course).

> What about performance?

Did you measure the performance impact of all those if/else's?

Exceptions are a special channel for errors, so your question is kind of
like "is stderr any good? what about performance?"     
 
> New OOP features: I can go through my code and mark all my methods as public
> or private or whatever. No problem. But why would I? It will make classes
> easier to understand when I look at them, but that's just convenience. What
> are the performance benefits? I've not found a single mention of that
> anywhere. What do abstractions and interfaces actually do, aside from
> structuring my code better?
 
What do PHP4 classes actually do for you, aside from structuring your code?
What about performance? Wouldn't you be better off if you wrote all of your
program into a single file, all in global scope, using only builtin functions
and primitive data types?  It would surely be faster, and you'd only lose the
convenience, no?

> What major compelling reasons do I have to start using exceptions and OOP-5?

All the things you mentioned, and then some.

Someone else mentioned that PHP 5 is much less inclined to copy objects.
You still don't get the convenience of a private copy constructor, but
hey.

Another thing is destructors, so you're able to mimic C++'s powerful

    // unlocked
    {
        mylock_t lock;
        // locked
    }
    // unlocked

(not so powerful in PHP without anonymous scopes).

For example, a unit-testing library for PHP 5 called Testilence provides two
utility classes, a temporary dir and a temporary file (see mkdtemp(3),
mkstemp(3)). Both classes remove the underlying filesystem objects in their
destructors, so you can conveniently skip doing the cleanup yourself:

    function test_O_EXCL_ThrowsOnExistingPath()
    {
        $file = $this->mkstemp();
        $this->willThrow('RuntimeException');
        new SplFileObject($file->path(), 'x+');
    }

Also, notice how the code can omit checking for errors in mkstemp().
The return value is guarranteed to be the right thing, since any errors would
be signalled by throwing an exception, and that is handled By the caller of
this method.

How about iterators? You can have objects that look like arrays yet they take
much less memory:

    $rs = $db->query($select); # query the db
    foreach ($rs as $row) { # fetch the row
        whatever($row);
    }

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux