Skip to content

Instantly share code, notes, and snippets.

@Wpkenpachi
Last active December 6, 2019 03:43
Show Gist options
  • Save Wpkenpachi/e4f76f20d668fb42d56fc67df4e404fd to your computer and use it in GitHub Desktop.
Save Wpkenpachi/e4f76f20d668fb42d56fc67df4e404fd to your computer and use it in GitHub Desktop.
<?php
// Model
namespace Model;
abstract class Connection {
protected $con;
function __construct () {
$this->conn = "Database Conn"; // Aqui fazer a conexão com o banco
}
}
abstract class Model extends Connection {
protected $table;
function __construct () {
$table = $this->resolveTableName(get_called_class());
$this->table = $table ?? null;
}
private function resolveTableName ($class) {
$payloadString = explode("\\", $class);
$hasNamespace = count($payloadString) > 1 ? true : false;
$table = null;
if ($hasNamespace) {
$lastClassName = count($payloadString) - 1;
$table = strtolower($payloadString[ $lastClassName ]);
} else {
$table = strtolower($class);
}
return $table;
}
public function select () {
echo "SELECT * FROM table";
}
public function update () {
echo "UPDATE table SET field = ?";
}
public function insert () {
echo "INSERT INTO table (...fields) VALUES(...values)";
}
public function delete () {
echo "DELETE FROM table WHERE field = ?";
}
public function getTableName () {
return $this->table;
}
public function getConn () { return $this->con; }
}
class User extends Model {}
// Controllers
namespace Controller;
use Model\User;
class UserController {
public function getUserTableName () {
$user = new User();
return view(["table" => $user->getTableName()]);
}
}
function view ($data) {
$viewExample = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1> Tabela: {$data["table"]} </h1>
</body>
</html>
HTML;
return $viewExample;
}
// Retorno ao Client
$userController = new UserController();
$payload = $userController->getUserTableName();
exit($payload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment