Archive for the ‘php’ Category

memcache is on holiday — please call back later

Freitag, Februar 27th, 2009

PHP Warning:  Memcache::pconnect(): Can't connect to localhost:11211, Unknown error (0) in CacheControl.inc.php on line 343, referer: http://www.google.com/search?&q=...

Why does this have to happen? You would think that the task of „creating“ an already exising connection, or merely returning the pointer to it, would be an easy one.

More often than not, it isn’t. We are just talking 1 out of 50.000 or so, but still enough to make quite an impact on the error files if you just scale PI. That will be it for tomorrow, let’s see who succeeds in the end — m(a/e)n or [unspeakable] machine.

Git — The fast version control system

Donnerstag, Februar 5th, 2009

Today at work we gave Git a first shot as the new version control system for a rather big PHP project.

The decision was mainly based on the (documented and hoped for) ease of use, the abilities, and excellent reputation. If its good enough for Linus and the Linux Kernel, it can’t really be bad for us. To be honest, a certain degree of curiousness was involved, too.

First steps contained taking the current live and development version as a basis for the new repository, with the plan to branch as often as applicable to be able to maintain a „live“ branch by simply merging features from the single branches into it when required.

For the first day this went quite well although the stable built of the Eclipse plug-in (update site) did not work at all leaving us using the „integration“ release for now.

The process is really as easy as exemplarily shown in the documentation

$ cd (project-directory)
$ git init
$ (add som files)
$ git add .
$ git commit -m 'Initial commit'

At the beginning I always forgot to add the files and wondered why the commit would not happen to contain any difference — but that should only be a question of getting used to it. Looks fine, for now.

Strategy pattern á la „Head First Design Patterns“

Sonntag, Februar 1st, 2009

<?php

/**
* Strategy design pattern example realized in PHP
*
* Inspired by „Head First Design Patterns
*
* @author <php@tschagrass.de>
*/
abstract class Animal {

protected $soundBehavior;
protected $moveBehavior;

public function performSound() {
$this->soundBehavior->performSound();
}
public function performMove() {
$this->moveBehavior->performMove();
}
public function setMoveBehavior($moveBehavior) {
if ($moveBehavior instanceof MoveBehavior) {
$this->moveBehavior = $moveBehavior;
}
}
}

// ——

interface SoundBehavior {
public function performSound();
}

interface MoveBehavior {
public function performMove();
}

// ——

class Barking implements SoundBehavior {
public function performSound() {
echo „I’m Barking \n“;
}
}

class Meowing implements SoundBehavior {
public function performSound() {
echo „I’m Meowing \n“;
}
}

class Doggystyling implements MoveBehavior {
public function performMove() {
echo „I’m Doggystyling \n“;
}
}

class Catwalking implements MoveBehavior {
public function performMove() {
echo „I’m Catwalking \n“;
}
}

class Jumping implements MoveBehavior {
public function performMove() {
echo „I’m Jumping \n“;
}
}

// ——

class Dog extends Animal {
public function __construct() {
$this->soundBehavior = new Barking();
$this->moveBehavior = new Doggystyling();
}
}

class Cat extends Animal {
public function __construct() {
$this->soundBehavior = new Meowing();
$this->moveBehavior = new Catwalking();
}
}

// ——

function makeAninimal() {
$i = rand(0, 1);
if ($i == 0) {
return new Dog;
}
return new Cat;
}

echo „<pre>“;
$animal = makeAninimal();
$animal->performSound();
$animal->performMove();
$animal->setMoveBehavior(new Jumping());
$animal->performMove();
echo „</pre>“;