Skip to content

Instantly share code, notes, and snippets.

@jcroot
Last active April 3, 2021 16:19
Show Gist options
  • Save jcroot/567a964f319e4b9ae1728dcac7292809 to your computer and use it in GitHub Desktop.
Save jcroot/567a964f319e4b9ae1728dcac7292809 to your computer and use it in GitHub Desktop.
Clase Abstracta
<?php
abstract class Database implements DatabaseCRUD {
}
<?php
interface DatabaseCRUD
{
public function fetchAll($where = null, $order = null, $limit = null);
public function find($id);
public function save(DatabaseModel $model, $params);
}
interface DatabaseModel {
public function insert($params);
public function update($id, $params);
}
<?php
class Main extends Database
{
public function saveUser()
{
$params = [
'id' => 1,
'name' => 'Fred',
'address' => 'Rocky Hard 112'
];
$this->save(new User(), $params);
}
public function saveProduct()
{
$params = [
'id' => 1,
'brand' => 'Pepsico',
'name' => 'Lays'
];
$this->save(new Product(), $params);
}
public function fetchAll($where = null, $order = null, $limit = null)
{
// TODO: Implement fetchAll() method.
}
public function find($id)
{
// TODO: Implement find() method.
}
public function save(DatabaseModel $model, $params)
{
// TODO: Implement save() method.
if ($model->getId()){
$model->update($model->getId(), $params);
}else{
$model->insert($params);
}
}
}
<?php
class Product implements DatabaseModel
{
protected $id;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function insert($params)
{
// TODO: Implement insert() method.
return $this->getDbTable->insert($params);
}
public function update($id, $params)
{
// TODO: Implement update() method.
return $this->getDbTable()->update($id, $params);
}
}
<?php
class User implements DatabaseModel
{
protected $id;
protected $name;
protected $address;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getAddress()
{
return $this->address;
}
public function setId($id)
{
$this->id = $id;
}
public function setName($name)
{
$this->name = $name;
}
public function setAddress($address)
{
$this->address = $address;
}
public function insert($params)
{
// TODO: Implement insert() method.
return $this->getDbTable->insert($params);
}
public function update($id, $params)
{
// TODO: Implement update() method.
return $this->getDbTable()->update($id, $params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment