Archiv

Archiv für die Kategorie ‘programming language’

Multiple Inheritance in PHP

20. Dezember 2009 Keine Kommentare

With the magic of __call() it is possible to do multiple inheritance in PHP. This example is more then a mixin pattern. It can call protected and private methods from inherited classes, too.

<?php

abstract class Inheritance {
    private $_inheritances = array();

    public function __call($method, array $arguments = array()) {
        // Here you can handle the calls. Maybe you don't want to call
        // every found method but only the first or the last ...
        foreach ($this->_inheritances as $inheritance) {
            $inheritance->invoke($method, $arguments);
        }
    }

    public function invoke($method, $arguments) {
        if (method_exists($this, $method) === true) {
            return call_user_func_array(array($this, $method), $arguments);
        }
    }

    protected function _addInheritance(Inheritance $inheritance) {
        $this->_inheritances[get_class($inheritance)] = $inheritance;
    }
}

That’s all! Now the usage:

We have two simple base classes, Bird and Horse, which have some methods.

/**
 * first base class
 */
class Bird extends Inheritance {
    public function chirp() {
        echo "<br/>chirp ..";
    }

    public function fly() {
        echo "<br/>fly ..";
    }

    protected function _eat() {
        echo "<br/>eat worms ..";
    }
}

/**
 * second base class
 */
class Horse extends Inheritance {
    public function whinny() {
        echo "<br/>whinny ..";
    }

    protected function _eat() {
        echo "<br/>eat hay ..";
    }
}

Now we want a new class named Pegasus that implements all methods from Bird and Horse. The only thing we have to do is to inherit from Inheritance and add the two base classes with _addInheritance().

class Pegasus extends Inheritance {
    public function __construct() {
        // bind extented classes
        $this->_addInheritance(new Bird);
        $this->_addInheritance(new Horse);
    }

    public function eat() {
        // works with protected methods, too
        $this->_eat();
    }
}

$pegasus = new Pegasus;
$pegasus->chirp();
$pegasus->fly();
$pegasus->whinny();
$pegasus->eat();

Output:

chirp ..
fly ..
whinny ..
eat worms ..
eat hay ..
Kategorienphp

Naming of Classes, Methods and Member Variables

18. Oktober 2009 Keine Kommentare

This article is not about syntax style. It’s only about right naming of classes, their methods and their member variables. Because there’s nothing better for other programmer (ie. like me) to see a constant name schema.

General Provisions

  • leave out articles like ‘a’ and ‘the’ – these are totally unnecessary (TheCar)
  • don’t use ‘my’ in the name – it’s not yours (myVar, MyClass)

Classes

  • try to use a noun or a short phrase with a noun (Tree, SubTree)
  • always use singular instead of plural
  • avoid imperatively ‘object’ in class names – classes are not a objects. You can find bad examples everywhere: stdObject, ArryObject, Zend_Date_DateObject

Methods

  • the name should simply describe what does the method do
  • try to use a verb or verb with a noun, like save(), build(), getTitle()

Member Variables

  • if type of boolean, then prepend ‘is’
  • avoid variables like ‘x’, ‘i’ – use for example ‘index’

jCharacterfall

15. Oktober 2009 Keine Kommentare

This is the first article of my new section “javascript/hacking”.

Firefox and and the Firebug add-on are required for the most scripts, I will post from time to time.

First go to this site http://demo.marcofolio.net/jcharacterfall/, put the code at the bottom in the console and run it. Then start the game.

Have much fun!

$(function () {
    var still = [];
    var $document = $(document);

    window.setInterval(function () {
        var $drop = $('#waterfall .fallingchar:last-child');
        var id = $drop.attr('id');

        if ($drop.length !== 1 || $.inArray(id, still) !== -1) {
            return true;
        }

        var letter = $.trim($('p', $drop).text());

        $document.trigger({
            type: 'keydown',
            keyCode: String.charCodeAt(letter)
        });

        still.push(id);
    }, 1);
});
Kategorienhacking

Chaining Variables

21. Juni 2009 Keine Kommentare

Do you have always asked yourself how to chain variables to make a sentence?

In the magic __get()-method the variable names will be collected, and finally the echo “calls” the __toString()-method.

<?php

class Sentence {
    protected $_words = array();

    public static function create() {
        return new self;
    }

    public function __get($word) {
        $this->_words[] = $word;

        return $this;
    }

    public function __toString() {
        return implode(' ', $this->_words) . '.';
    }
}

Usage:

echo Sentence::create()->All->your->base->are->belong->to->us;

Output:

All your base are belong to us.
Kategorienphp