PHP OO Performance when generating thousands of an object type

439 Views Asked by At

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

0

There are 0 best solutions below