Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Created June 14, 2020 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bappi-d-great/c6fdc7b4b59cb45d46a51cfbee4c300b to your computer and use it in GitHub Desktop.
Save bappi-d-great/c6fdc7b4b59cb45d46a51cfbee4c300b to your computer and use it in GitHub Desktop.
Simple Model Class
<?php
class Model {
protected function __construct() {
parent::__construct();
}
public static function get_instance() {
static $Inst = null;
if( null === $Inst ) {
$Inst = new self();
}
return $Inst;
}
public function save() {
$vars = get_object_vars( $this );
$data = [];
foreach( $vars as $key => $val ) {
if( in_array( $key, $this->excluded ) || 'excluded' === $key || 'table' === $key ) continue;
$data[$key] = $val;
}
$sql = 'INSERT into ' . $this->table . ' (`' . implode( array_keys( $data ), '`, `' ) . '`) VALUES(\'' . implode( $data, '\', \'' ) . '\')';
echo $sql;
}
}
class People extends Model {
public $table;
public $name;
public $age;
public $job;
public $excluded = [];
protected function __construct() {
$this->table = 'people';
$this->name = 'Ash';
$this->age = 32;
$this->job = 'Developer';
$this->excluded[] = 'age';
}
public static function get_instance() {
static $Inst = null;
if( null === $Inst ) {
$Inst = new self();
}
return $Inst;
}
}
$people = People::get_instance();
$people->save();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment