我正在创建动态crud,但我希望将cast方法(数据库数组到当前类的对象)直接继承到我需要获取名称引用的子项,但是我继承了该方法的对象,如果我把它放在继承的类中,它的工作是完美的,但如果我有1万个继承的类,我必须复制1万次(那是我想要避免的)
<?php
namespace Models;
use Interfaces\ICrud;
use db\DBconection;
require('./interfaces/icrud.php');
require('./db/dbconnect.php');
abstract class Model implements ICrud{
private $connection;
protected $schema='public.';
public function __construct(){
}
public function get($id){
return (new DBconection())->runQuery("SELECT * from $this->schema".$this->getModelName(get_class($this))." WHERE id=$id");
}
public function create($entity){
}
public function update($entity){
}
public function delete($id){
return (new DBconection())->runQuery("DELETE from $this->schema".$this->getModelName(get_class($this))." WHERE id=$id");
}
private function getModelName($class){
return strtolower(str_replace('Model','',(new \ReflectionClass($class))->getShortName()));
}
}
这是儿童班
<?php
namespace Models;
use Models\Model,
db\DBconection;
require('model.php');
class UsersModel extends Model {
private $id;
private $username;
private $password;
private $active;
private $create;
private $update;
private $id_typeuser;
private $id_people;
protected $schema='security.';
public function __construct(){
}
}