Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- abstract class Model {
- public function get_object_name()
- {
- return strtolower(get_class($this));
- }
- public function get_props()
- {
- $object_props = get_object_vars($this);
- return $object_props;
- }
- public function create()
- {
- $object_props = $this->get_props();
- $columns = "(";
- $values = "(";
- foreach($object_props as $key=>$value)
- {
- $columns.=$key.',';
- $values.=$value.',';
- }
- $columns.=")";
- $values.=")";
- $query_string = "INSERT INTO ".$this->get_object_name()." ".$columns." VALUES ".$values;
- echo $query_string;
- }
- public function delete()
- {
- $object_props = $this->get_props();
- $columns = "(";
- $values = "(";
- foreach ($object_props as $key => $value) {
- $columns.=$key.',';
- $values.=$value.',';
- }
- $columns.=")";
- $values.=")";
- $query_string = "DELETE FROM ".$this->get_object_name()." "." WHERE ".$columns."=". $values;
- echo $query_string;
- }
- public function update()
- {
- $object_props = $this->get_props();
- $columns = "(";
- $values = "(";
- foreach ($object_props as $key => $value) {
- $columns.=$key.',';
- $values.=$value.',';
- }
- $columns.=")";
- $values.=")";
- $query_string = "UPDATE ".$this->get_object_name(). " SET ".$key. "=".$value." WHERE ".$columns." = ".$values;
- echo $query_string;
- }
- public function select(){
- $object_props = $this->get_props();
- $columns = "(";
- $values = "(";
- foreach ($object_props as $key => $value) {
- $columns.=$key.',';
- $values.=$value.',';
- }
- $columns.=")";
- $values.=")";
- $query_string = "SELECT ".$columns. "FROM ".$this->get_object_name();
- echo $query_string;
- }
- public function select_array(){
- $object_props = $this->get_props();
- $query_string = "SELECT * FROM ".$this->get_object_name()."WHERE ";
- $items = array($columns =>$values);
- $array_length = count($items);
- foreach ($items as $key => $value) {
- $index = array_search($key,array_keys($items));
- $query_string.=$key.'='.$value;
- if ($index!=$array_length-1) {
- $query_string.='AND';
- }
- }
- echo $query_string;
- }
- }
- /*class Auth
- {
- static public function authenticate($credentials = array('' => , );)(
- )
- }*/
- class Db
- {
- public $servername = 'localhost';
- public $username = 'root';
- public $password = 'passme@123';
- public $dbname = 'testphp';
- public function __construct()
- {
- $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
- if($conn->connect_error)
- {
- die("could not connect". $conn->connect_error);
- }
- $this->conn = $conn;
- return $this;
- }
- function query($query)
- {
- $this->conn->query($query);
- }
- }
- /*function createUsers(){
- $dbase = new Db;
- $checkTable = "CREATE TABLE PostTable(
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- firstname VARCHAR(30) NOT NULL,
- messageBody VARCHAR(80) NOT NULL,
- reg_date TIMESTAMP)";
- if ($dbase->query($checkTable) === TRUE) {
- echo "Table PostTable created successfully";
- } else {
- echo "Error creating table: " . $dbase->error;
- }
- if($checkTable !== FALSE){
- echo "table already exists, moving on ";
- }else{
- return $checkTable;
- }
- $dbase->conn->close();
- }*/
- class User extends Model {
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement