pl: Implementierung eines Interface und Vererbung

Beitrag lesen

MB baut einen generischen SQL Builder.

Bitteschön:

<?php

interface SqlString{    
    function select();
    function from();
    function where();
    function order();
    function limit();
}

# Basisklasse
class SqlBase{
    # SQL Fragmente erfassen
    function __construct($frag){
        $this->FRAG = $frag;
        $hthis->SQL = '';
    }
    function __toString(){
        return $this->SQL;
    }
}

class Moon extends SqlBase implements SqlString{
    function select(){
        $this->SQL .= "SELECT ".join(", ", $this->FRAG['fields']);
    }
    function from(){
        $this->SQL .= " FROM ".$this->FRAG['tabn'];
    }
    function where(){
        $this->SQL .= " WHERE julianday > ".$this->FRAG['julianday'];
    }
    function order(){
    }
    function limit(){
        $this->SQL .= " LIMIT ".$this->FRAG['limit'];
    }
}


# SQL zum Auslesen der Mondphasen
$m = new Moon(array(
    'julianday' => 2440588,
    'tabn'     => 'moon',
    'fields'   => array('julianday','phase'),
    'limit'  => 10
));

# Rufe die Methoden des Interface auf
$m->select();
$m->from();
$m->where();
$m->order();
$m->limit();

# SQL String Ausgeben
echo $m;

SELECT julianday, phase FROM moon WHERE julianday > 2440588 LIMIT 10

Genau deswegen ja bauen wir ein Interface, damit wir beliebiges SQL generieren. Es kommt darauf an das Prinzip und die Wirkungsweise eines Interface zu verstehen!

MFG