[nycphp-talk] OOP noob - general best practices

Michael B Allen ioplex at
Wed Jan 20 16:46:30 EST 2010

On Wed, Jan 20, 2010 at 1:48 PM, Eddie Drapkin <oorza2k5 at> wrote:
> On Wed, Jan 20, 2010 at 1:42 PM, Michael B Allen <ioplex at> wrote:
>> Unless you need polymorphic behavior where extending a type so that it
>> inherits the behavior of another is clearly better, it is very likely
>> that you can achieve the same objective more efficiently without using
>> objects at all. This is particularly true in PHP because of the array
>> type. The array type in PHP is a builtin type and a generic ordered
>> but hashed container. So if you just need a context to maintain some
>> state with only one reference - an array is a good option.
> An array and an object are almost identical in PHP.  Other than
> syntactically, as far as resource usage, there's no difference
> between:
> $a->foo = $bar; //well, you get an E_STRICT here, but you can $a = new
> stdClass() to prevent that.
> $a['foo'] = $bar;
> Internally, PHP objects are hashes as well, so there's no really good
> argument to using arrays over objects.

Fact: In PHP, creating and using an object requires more resources
than creating and using an array. There are a variety of reasons for
this but the two most significant that spring to mind are because
creating an object requires loading the class definition and because
the codepaths for the object are unique as opposed to an array which
is virtually guaranteed to be hot in the cache (meaning the array will
exhibit better cache locality). Also $a->foo = <something> could
trigger code in a couple of ways so to say there is "no difference"
between that and $a['foo'] = <something> is largely false.

As for the advantages of OOP, that is a highly subjective topic for
which I have stated my opinion and have no interest in getting into a
long discussion about.


Michael B Allen
Java Active Directory Integration

More information about the talk mailing list