"PHP 5: NOT EVEN CLOSE"

This document is http://tnx.nl/php5 - If you replicate it, please do link.
Trust HTTP headers for last-modified info. Corrections and additions are
welcome at geeks#@tnx.nl.

Existing problems not addressed

Yes, some bugs were fixed in PHP 5, but none of the issues described
in http://tnx.nl/php was addressed. PHP is
still a world of inconsistency, still lacks advanced scoping, still has a
bloated core, etcetera.

The rest of this document will be a discussion of the language changes
in http://www.php.net/zend-engine-2.php.
Again, it will mainly be a comparison with Perl. In general, this means that
you'll be told that Perl already had the feature ages ago :)

Oh, please note that I am not an OO expert and will not claim that anything is
or is not "pure OO".

New Object Model

PHP has a new object model. This is the first really useful incarnation of
object orientation in PHP. Without doubt, the changes made are very valuable to
PHP and were all inspired by the needs of PHP coders.

There is a big philosophical difference between PHP and Perl, that makes the
object models hard to compare. In Perl, the object model is extremely flexible
and simple, and doesn't force the user into any mindset. It does not have
special syntax, but re-uses other parts of the language. A class is just a
package, a method is just a sub, an object is just a reference to a blessed
data structure.

Private and Protected Members/Methods

PHP 5 introduces the private and protected keywords. Perl
does not have these. It also doesn't have any keywords that are equivalent.
That does not mean that there is no equivalent. Perl lets you check these
things yourself with the caller operator, which returns information
about the calling context.

In general, private and protected things are rare in Perl. Many Perl coders
believe in freedom, and only prefix an underscore as a visual indication. The
perlmodlib document says:

Perl does not enforce private and public parts of its modules as you may have
been used to in other languages like C++, Ada, or Modula-17. Perl doesn't
have an infatuation with enforced privacy. It would prefer that you stayed out
of its living room because you weren't invited, not because it has a
shotgun.

Because some masochists do like painting themselves into a corner, Perl 6 will
support these features.

Abstract Classes and Methods, Interfaces

In Perl, you create these yourself. Nothing special. It should be noted that
Perl supports multiple inheritance, while PHP does not. There are those who
think multiple inheritance is evil, and there are those who know it can be a
useful tool in some situations. Perl does not limit the people who think it's
useful.

Perl 6 will have special syntax for these features.

Class Type Hints

Perl has no types, period. However, an object's class can be checked manually.

I find it strange that although PHP does have types, this mechanism cannot be
used to test built-in types.

Perl 6 will have types and subroutine signatures.

Final

PHP lets you block inheritance. Perl does not provide this option at all.

Some languages solve this (or think they solve it) by letting classes declare
themselves to be closed and/or final. But that's actually a bad violation of OO
principles. It should be the users of a class that decide such things--and
decide it for themselves, not for others. As such, there has to be a consensus
among all users of a class to close or finalize it. And as we all know,
consensus is difficult to achieve.

Still, because the Perl wants to let people free in how they code, Perl 6 will
support this feature.

Object Cloning

A PHP object can now be cloned. Not with $object->clone, as I would
have expected, but with clone $object.

Perl has real references, and objects are just data structures. Because
circular references are possible, and perl cannot tell if a certain data
structure is part of the object, it cannot provide a default cloning method. A
class has to provide one.

PHP has just taken another step towards never being able to support real
references.

Unified Constructors

This is just a philosophy change. Perl has a different philosophy than PHP 4
and PHP 5.

Destructors

PHP finally has destructors. Perl has had destructors for a very long time. Now
if only PHP had lexical scope, the destructors would be really useful! But
alas, it doesn't sport such classy extras. Instead, only explicit
destruction and destruction at the end of the function or script exist.

Constants

PHP now has constants in classes. Nothing special, and something that Perl coders
usually laugh about: why are classes so different from "normal" code?

Exceptions

Perl has had exception handling all along, but with unusual syntax. Throwing an
exception is done with die, catching it is done with eval { ...
} (which is neither slow nor dangerous) and if ($@) { ... }. And
for those who want very complex exception mechanisms, there are several
extensions available from CPAN.

Dereferencing objects returned from functions

Believe it or not, in PHP 4, it was impossible to call a method on a returned
object. That also made it impossible to stack methods, and essentially made OO
programming a pain in the ass. Again, Perl coders laughed: why make OO
programming different from the rest of the language, if you can merge the two
and get all features for free?

Static Methods

Because in Perl a method is just a sub that gets passed one extra argument,
calling methods from outside object oriented context has always been possible.

Instanceof

In Perl, this is called isa and it has been around for a very long
time. (Read it as is-a.) I find it funny that PHP has this as an infix
operator, instead of using object oriented syntax. Can the internals of PHP
still not really handle OO?

PHP: if ($foo instanceof Bar) ...
Perl: if ($foo->isa('Bar')) ...

TODO: find out if the class has to be a literal (none of the PHP examples are
like $foo instanceof $bar, while in Perl it is perfectly valid to say
$foo->isa($bar).

__autoload()

This is not Perl's AUTOLOAD, but something that is called when a class that
does not exist is used. In Perl, this has been possible for a long time, and
there even is a module that automatically loads modules on demand using this mechanism.

Overloadable Method calls and Property accesses

Perl has many, many ways of doing this. But, I have to admit that in general I
like how PHP does __get and __set better (just not the names).

Iteration

Perl has several ways of letting an object be iterable

New __METHOD__ constant

Perl doesn't have this as a constant (partly because the method name isn't
always known at compile time), but allows you to retrieve the current sub's
name through caller.

New __toString() method

Hah, they call this magic! PHP lets the coder specify how their objects should
be stringified, but provides no way of using it as an array or number.

Perl has a very sophisticated overloading mechanism that lets you overload an
object differently in different contexts.

Reflection API

Perl has always been this flexible, just in a different way

Conclusion

Life has just been made much better for PHP programmers. But the real, lower
level issues remain. Adding an object model doesn't fix the shortcomings that
exist throughout the language.

And they still don't support real OO syntax for their built in objects like
MySQL "resources".

PHP 5 isn't a BIG improvement. I wonder why it deserves a new major version
number.