Klassen von PHP erstellen lassen
In der Regel programmieren wir ja, um anderen die Arbeit leichter zu machen, bzw. erst zu ermöglichen.
Heute möchte ich einen Weg vorstellen, wie wir uns das Leben etwas leichter machen können.
Ich habe dazu zwei Klassen Online gestellt ClassCreater und TemplateCreater.
Wie der Name schon sagt, erstellt dieses Projekt automatisch Klassen und das dazugehörige Template.
So enthält die damit generierte Klasse alle nötigen Methoden um Daten
in einer Datenbank anzulegen, zu bearbeiten und zu löschen.
Das Projekt befindet sich zwar noch im Entwicklungsstadium, dennoch ist der generierte PHP Code
bereits voll funktionstüchtig.
Die Klassen sowie das Template werden anhand eines create table statements generiert->
BSP:
create table contact (
id int(12) unsigned not null auto_increment,
firstname varchar(255) default null,
lastname varchar(255) default null,
email varchar(255) default null,
birthday datetime NOT NULL default '0000-00-00 00:00:00',
comment text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Und dazu passend hier die generierte Klasse:
Update: Die Security issues sollten jetzt behoben sein !
Code
<?php
class contact extends BaseApp {
/**
* @var int
*/
public $id = 0;
/**
* @var string
*/
public $firstname = '';
/**
* @var string
*/
public $lastname = '';
/**
* @var string
*/
public $email = '';
/**
* @var string
*/
public $birthday = '';
/**
* @var string
*/
public $comment = '';
protected $db;
/**
* The constructer
*/
public function __construct ($id = '')
{
if (!isset($this->db))
$this->db = parent::getInstance();
if (!empty($id))
{
$this->id = $id;
$this->get();
}
}
/**
* Sanitize a input array
*
* @param array $data
* @return array
*/
public function check($data = '')
{
$check = array(
'id' => FILTER_SANITIZE_NUMBER_INT,
'firstname' => FILTER_SANITIZE_STRING,
'lastname' => FILTER_SANITIZE_STRING,
'email' => FILTER_SANITIZE_STRING,
'birthday' => FILTER_SANITIZE_STRING,
'comment' => FILTER_SANITIZE_STRING
);
return filter_var_array($data, $check);
}
/**
* Delete a element in the Database
*/
public function del ()
{
if (empty($this->id) || !is_numeric($this->id))
return false;
$sql = "DELETE FROM contact WHERE id = :id";
$stmt = $this->db->dbh->prepare($sql);
$stmt->bindValue(':id', $this->id, PDO::PARAM_INT);
$stmt->execute();
}
/**
* Get a list of objects
*
* @param array $data optional if you like to get only a subset of data
*
* @return obj
*/
public function get_list ($data = '')
{
$sql = "SELECT c.id, c.firstname, c.lastname, c.email, c.birthday, c.comment FROM contact as c";
if (is_array($data))
{
$data = $this->check($data);
$sql .= " WHERE";
foreach ($data as $key => $value)
{
if ($value)
$sql .= " c.$key = '$value' and";
}
$sql = substr($sql, 0, -4);
}
$stmt = $this->db->dbh->query($sql);
$obj = $stmt->fetchALL(PDO::FETCH_CLASS, 'contact');
return $obj;
}
/**
* Get one Dataset from the db
*/
public function get ()
{
if (empty($this->id) || !is_numeric($this->id))
return false;
$sql = "SELECT c.id, c.firstname, c.lastname, c.email, c.birthday, c.comment FROM contact as c WHERE
c.id = $this->id";
$stmt = $this->db->dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
foreach ($result as $key=>$val)
{
$this->$key=$val;
}
}
/**
* The save method is responsability for
* saving and updating a dataset
*/
public function save ()
{
if (empty($this->id))
{
$sql = 'INSERT INTO contact (firstname, lastname, email, birthday, comment) VALUES
(:firstname, :lastname, :email, :birthday, :comment)';
$stmt = $this->db->dbh->prepare($sql);
}
else
{
$sql = 'UPDATE contact set
firstname = :firstname,
lastname = :lastname,
email = :email,
birthday = :birthday,
comment = :comment';
$sql .= " WHERE id = :id";
$stmt = $this->db->dbh->prepare($sql);
$stmt->bindValue(':id', $this->id, PDO::PARAM_INT);
}
$stmt->bindValue(':firstname', $this->firstname, PDO::PARAM_STR);
$stmt->bindValue(':lastname', $this->lastname, PDO::PARAM_STR);
$stmt->bindValue(':email', $this->email, PDO::PARAM_STR);
$stmt->bindValue(':birthday', $this->birthday, PDO::PARAM_STR);
$stmt->bindValue(':comment', $this->comment, PDO::PARAM_STR);
$stmt->execute();
}
}
?>
<?
include('./inc/class/BaseApp.php');
include('./inc/class/contact.php');
# ein eintrag erstellen
$new_contact = new contact();
$new_contact->firstname = "andreas";
$new_contact->save();
# ein eintrag aktualisieren
$contact = new contact(1);
$contact->firstname = "andreas";
$contact->lastname = "beder";
$contact->save();
# nun laden wir eine liste, mit gewissen eigenschaften
$list = $contact->get_list(array("firstname" => "andreas", "lastname" => "beder"));
foreach($list as $obj)
{
# das aktualisieren geht ganz einfach
$obj->email="andreas@codejungle.org";
$obj->save();
# ebenso das löschen
$obj->del();
}
?>