Created
June 14, 2020 12:48
-
-
Save bappi-d-great/c6fdc7b4b59cb45d46a51cfbee4c300b to your computer and use it in GitHub Desktop.
Simple Model Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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