Archive for December, 2007

OOP in PHP (part II)

Sunday, December 16th, 2007

Time to discover the hidden powers of the object oriented programming in PHP. Today I’ll describe the usage of serialisation, the secrets of overloading and - probably the most useful thing today - design patterns.

Serialisation

As you remember from the first part of the article, objects are completely different type of variables. You could easily write a string, number or even an array to a file… but what about objects? You can’t just write an object to a file, as PHP will try to convert it to a string, which is technically impossible, and will result in throwing an error. Yeah, I guess you can already suppose that there’s a solution.

Actually, all you have to do to serialise an object (i.e. make it writable to a file) is use one function: serialize(). As you can guess, using unserialize() function will allow you to have your object back once it has been saved to a file. Remember to define the object’s class before unserialising it, otherwise it will become useless.

Methods __sleep() and __wakeup() make serialisation in PHP5 even easier. They are called, respectively, before an object is serialised or unserialised. They are very useful e.g. for closing and reopening database connections, or saving additional information about the serialisation process. Remember that the __sleep() magic function has to return an array of all members that should be serialised.

class AutoStalker {
   private $filename, $file;

   public function __construct($filename) {
      $this->filename = $filename;
      $this->open();
   }

   public function prepare_for_stalking() {
      $this->file = fopen($this->filename, 'a');
   }

   public function stalk($text) {
      fwrite($this->file, $text."\n");
   }

   public function see_whos_been_stalked() {
      return file_get_contents($this->filename);
   }

   public function __wakeup() {
      $this->prepare_for_stalking();
   }

   public function __sleep() {
      fclose($this->file);
      return array('filename');
   }
}

In the example above, I only serialise the file’s name, because we don’t need any additional information to open that file in the future.
Now, let’s see something more useful; how about using some PHP cookies?

(more…)

Please wait, loading…

Saturday, December 15th, 2007

Loading. The inevitable part of any gamer’s life. There are many different ways of fighting it – some developers decide to limit the number of textures and objects to load, others try making two-dimensional backgrounds, and Sony doubles the internal memory of their PlayStation Portable. But why do some games need much more time to load than others with similar graphics? The answer is: code optimisation.
Heroes of Might & Magic 4 loading screen

The wannabe developers often release games that require twice as much RAM or CPU power as they would if they were produced by, let’s say, Activision. They usually don’t optimise their code as much as the experienced developers would. Thus, you don’t see many games that take ages to load published on the 360, because it is extremely difficult to pack a game into one DVD disc and developers are forced to make their code as compact as possible.

PC gaming, however, isn’t limited by the capacity of a disc. Not only do the unexperienced developers fail to produce optimised games – it often happens to such giants as EA. Have you ever played Harry Potter and the Prisoner of Azkaban on your computer? I could easily make myself a cup of tea while a new level was loading. There are THOUSANDS of other games with such a poor performance, but complaining about their efficiency is pointless; you will always hear the “get a better PC” argument as an answer. World of Warcraft loading screen Of course, you can go to the shop and buy yourself a new graphic card, but wouldn’t it be better to spend this money on new games?

Obviously, it would. I don’t really like to see the new DirectX being implemented in computer games, I don’t really want my PC game to have outrageously great graphics, I don’t really want to be forced to change the game’s graphic details level to the minimum. I want my games to be working as fast as they were on the E3 demo when they were announced. And that’s why I value the next-gen consoles much more than the modern personal computers.

I do realise that this rant probably won’t convince the die-hard PC gamers to change their mind, but just think of how much cash you spend on new processors, graphic cards and RAM memory and how many games you could buy for it. In my opinion, PC gaming will eventually die, and now is the best time to abandon the sinking ship of hardware upgrades and join the gaming community with the brightest future.
Just get yourself a console.