2

I've been reading the Matt Zandstra PHP Object Oriented Designs and Patterns and quite liked an idea but was wondering how it effects PHP Performance when you create hundreds maybe thousands of them.

You have a map consisting of thousands of different type of tiles (like in civilization). You have a generic tile and then the other tiles may be one of 10 different types.

He was basically saying that you create a Basic tile and then have the other tiles inherit the properties like:

abstract class Grass {

    public movementSpeed = 1;

    public function getMovementSpeed () {
        return this.movementSpeed;
    }        

}

class Hill extends Grass {
    public movementSpeed = 0.5;
}

class Mountain extends Grass {
    public movementSpeed = 0.3;
}

class Lake extends Grass {
    public movementSpeed = 0.6;

    public function seaMonster () {
        // Kill between 0.2 - 0.4x of units with sea monster 
    }

}

You have all of the different types of terrains that make up this map (upon first generation) and then loop through the database to build it up like:

ID | TYPE
----------------- 
1 | grass
2 | mountain
3 | mountain
4 | grass
5 | lake
6 | grass

So I guess my question is... how does this go with performance? Does PHP store every tile as a big object or does it just contain a reference to the object definition?

I'm just thinking when its a map full of 1,000s of squares and I'm looping through and doing other jazzy stuff, will it be crazy slow as I always thought Object Oriented programming was quite bulky.

Would it be much quicker to have it procedural then loop through the squares and run functions to decide what happens with these?

Just wondering in general what performance is like when using an OO Design/Pattern like this?

Edit: What I mean by OO and Procedural looping is something like:

// Procedural

$tiles = array();

// Loop through DB of tiles
$tiles[] = $row->type;

$speed = getSpeed($tiles[0]);

// OO
$map = new Map;

// Loop through DB of tiles
switch ($row->type) {
   case 1:
       $map->addTile(new Plains);
       break;
   case 2: 
       $map->addTile(new Mountain);
       break;
}

$tile = $map->getTile(0);
$speed = $tile->getMovementSpeed();

As you can see, the OO method really makes sense, you're populating a map with tiles but for 2 mountains is there any sort of duplication or when you come to call it, does it just reference the class blueprint as to what its doing.

I guess it just calls the construct method (if there is one) and then you have it's methods on hand when you need them so its pretty quick right?

Thanks, Dom

hakre
  • 193,403
  • 52
  • 435
  • 836
creamcheese
  • 2,524
  • 3
  • 29
  • 55
  • I can't speak for PHP internals, but Python (which has a *really* elaborate object model, and no non-object types to begin with) with a JIT-compiling interpreter, the hot loops can become as fast or even faster (because it can optimize agressively, go fast as hell and fall back when the optimizations aren't valid any more) than hand-written C for most of the time. Heap allocations can be avoided completely and member access can be inlined even for objects already allocated elsewhere. The moral of the story is: Optimize the language implementation, don't cripple the programmer ;) –  Oct 16 '11 at 15:41
  • 2
    Given that landscape tiles are mostly uninteresting and unchanging in terms of behavior, you would probably not create one tile each, but just one of each type and then assign that to a grid. Also compare Flyweight pattern. Re memory: you should not care. Re Looping: improve your access strategy instead. Also, what is the difference between procedural looping and oo looping? – Gordon Oct 16 '11 at 15:55
  • Edited my post to show a sort of loop of how it would be used... I'm starting to think performance wouldn't really be hindered so much and it's making a little more sense – creamcheese Oct 16 '11 at 18:01

0 Answers0