Fog Creek Software
Discussion Board

PHP Design Flaws

After working with PHP for a while, I noticed a serious design flaw in the language: when you perform an object assignment, the object is copied as a whole, while you would expect only a reference to the object to be copied.

$myObj = new MyObject();
$myObj.prop = 1;
$yourObj = $myObj;
$yourObj.prop = 2;

There are now two objects instead of one, with two different property values. To avoid this, you apparently have to use =& instead of =. This leads to really hard-to-find bugs because it's so easy to forget the & everywhere.

This seems like a serious design flaw to me, since almost any other high-level language on the planet treats object variables as references. To make things worse, the = assignment makes a shallow copy, not a deep copy, so it's useless even if you do want to copy objects.

On a related note, PHP's parser displays hints of a one-dimensional implementation that is so common among mediocre programmers. For example, it will accept:

$child =& $myObj->getChild();

but not:


Any comments?

Frederik Slijkerman
Tuesday, October 22, 2002

"[W]hen you perform an object assignment, the object is copied as a whole" -- I thought that was "deep copy." Could you please explain a bit more?

Tuesday, October 22, 2002


Each object holds a tree of references.  It refers to things which may refer to others (ignoring circular refs).  A shallow copy is when you simply copy the values of your first object, so that you've got two objects that refer to the same things.  A deep copy goes down the tree and copies everything.

Tuesday, October 22, 2002

A worse design flaw in PHP is that there is no easy way to force the script to require variables declared.

In ASP you have "Option Explicit", and in Perl "use strict". But a standard installation of PHP has nothing like this.

Not having any way of forcing declarations means that a typo of a variable name is a nightmare to debug.

(Don't get me started on the crazy scoping rules PHP has with it's $GLOBAL arrays!)

Matthew Lock
Tuesday, October 22, 2002

The Zend engine version 2 (which you can download in beta) for PHP corrects some of these design flaws.  They have made all object assignment references and they handle compound statements:


They also changes how constructors are handled and added destructors. 

However, the copy-on-assignment thing isn't all that annoying once you get used to it.  For example, I find that I rarely actually need to assign a reference to an object.  (Of course, if your coming from another programming lauguage, it might be a hard habit to break).

Wayne Venables
Tuesday, October 22, 2002

In ASP you have "Option Explicit", and in Perl "use strict". But a standard installation of PHP has nothing like this.



That'll give you want you want. 

Wayne Venables
Tuesday, October 22, 2002

This situation just goes to show that getting the design perfect in 1.0 is a bad strategy. It's arguable that these types of design "flaws" are key drivers to php's tremendous success.

Wednesday, October 23, 2002

PHP is a horrible horrible language. The object model is crap, variable scoping is a nightmare, OO is insanity. Simply, if you are a software developer with experience in other languages PHP is a bag full of surprises.

In my opinion it has become hugely popular for 2 reasons:

1) C'ish syntax, so people doesn't run away scared as they do with Perl.

2) Huge native APIs to do just about everything, since most modules are native it's also pretty damn fast.

But as a language design I think they made every possible mistake imaginable.

Most of these problems you can live with, but there's one thing that personally drives me nuts. Variable scoping, if only they moved to a convention similar to that of Ruby, PHP would be bearable.

Beka Pantone
Wednesday, October 23, 2002

In response to Beka Pantone:

If it's so horrible, please answer the following questions:

1) Where is the equivalent of a PostNuke or similar (heavily used) content-management system written in a language for purists?

2) Where is the equivalent of a phpBB or similar heavily featured, easily customizable and outrageously fast discussion board written in a language for purists?

3) Why is PHP accelerating in usage in major IT shops (including HP, CGE&Y, Audi, Cisco, Deutsche Bank, Motorola, Southwestern Bell, NTT, Lufthansa, Mitsubishi, NASA, Deloitte, etc.) faster than any other language?

4) Why does PHP execute 3-4 times faster than JSP pages (source: eWeek) and has proven to be equally more efficient in development time (source: PerlMonth) - both factors resulting in far less costly development projects?

Where is the "pure CS" language when it comes to delivering real solutions?  Not being a smart-ass, where is it being deployed in a manner as efficient as the examples, above?

Dave M
Thursday, October 24, 2002

*  Recent Topics

*  Fog Creek Home