Ways that PHP4 sucks for work on non-trivial Object-Orientated problems
From Nick Jenkins
Ways that PHP 4 sucks for work on non-trivial Object-Orientated problems.
- No support for 'public', 'private', 'protected' data members or functions.
- Everything is public, so there is no way in PHP 4 to enforce black-box data abstraction.
- Objects are passed as copies by default, not as references by default.
- In Java, everything is an reference by default (apart from simple data types). This assumption works well when you are working with objects by default.
- Contrast with PHP, where everything is a copy by default. This assumption works well if you are working with simple data types (Strings, numbers, arrays), where you want to manipulate that data.
- However, the PHP assumption becomes very annoying when working with objects. This is because with objects, you usually want to modify the original object, not a copy of it.
- To work around this, you need to use two "&" characters, like this:
class itemStore { function &getItem ($item_index) { // Note the "&" character on this line // insert code here } } $item =& $item_store->getItem ($item_index); // Note the "&" character on this line
- If you forget those & characters, then you'll get a copy, not a reference. Any changes will be lost as soon as the object you modified falls out of scope. This makes a very easy way to introduce bugs, and it's made even more annoying by the fact that it is so silly - with objects, what you want 99% of the time IS a reference, not a copy.
- No destructors in PHP4.
- The "$this" syntax when working with classes is slightly annoying, and you have to use it all the time, even when not using it would still be unambiguous.
// In PHP: class foo { var $y; function setVal ($x) { $this->y = $x; } } // In Java: class foo { String y; function setVal (String x) { y = x; } }
- The "->" syntax is slightly annoying, and a throwback to C. The "." notation would be a better choice.
- I.e.
$object.doSomething();
looks nicer and is quicker to type that$object->doSomething();
It's only a small annoyance, and unfortunately it's probably too hard to change this now, due to backwards compatibility, and because the "." operator is already defined as the concatenation operator in PHP.
- I.e.
- No multiple inheritance.
- In some other languages, an object can inherit from multiple classes, but in PHP4 it's either inherit one class or inherit no classes. In some ways this isn't so bad though - C++ allows multiple inheritance, and there is a some experience to suggest that the added complexity for the compiler and the programmer outweighs the few situations where this behaviour is beneficial. As a result, some of the more modern languages have made a conscious decision to not support multiple inheritance, but to use single inheritance of concrete classes, combined with allowing classes to implement multiple interfaces.
However, all is not lost. PHP 5 is set to address most, but not all of these shortcomings. In particular:
- PHP5 automatically passes objects by reference.
- PHP5 has Private / Protected / Abstract / Final.
- Destructors are being added to PHP5.
- PHP5 does not have multiple inheritance, but it does have multiple interfaces instead.
Update: PHP5 was released on the 13-July-2004, although PHP5.1 looks like the far more polished release and is probably the one to use.