Fatal error : Uncaught TypeError: Argument 1 passed to Core\BaseModel::__construct() must be an instance of PDO, null given, called in C:\xampp\htdocs\Hotel_MVC\core\Container.php on line 15 and defined in C:\xampp\htdocs\Hotel_MVC\core\BaseModel.php:10 Stack trace: #0 C:\xampp\htdocs\Hotel_MVC\core\Container.php(15): Core\BaseModel->__construct(NULL) #1 C:\xampp\htdocs\Hotel_MVC\App\Controllers\FuncionarioController.php(21): Core\Container::getModel(‘Funcao’) #2 C:\xampp\htdocs\Hotel_MVC\core\Container.php(10): App\Controllers\FuncionarioController->__construct() #3 C:\xampp\htdocs\Hotel_MVC\core\Route.php(69): Core\Container::newController(‘FuncionarioCont…’) #4 C:\xampp\htdocs\Hotel_MVC\core\Route.php(10): Core\Route->run() #5 C:\xampp\htdocs\Hotel_MVC\core\bootstrap.php(5): Core\Route->__construct(Array) #6 C:\xampp\htdocs\Hotel_MVC\public\index.php(5): require_once(‘C:\xampp\htdocs…’) #7 {main} thrown in C:\xampp\htdocs\Hotel_MVC\core\BaseModel.php on line 10
Não sei direito PHP, mas pela mensagem, parece que foi passado um argumento do tipo errado para um construtor.
1 curtida
Cade o código aonde acontece esse problema, precisamos da sua classe e aonde chama ela!
Argumento não passado é o problema
class FuncionarioController extends BaseController{
public function __construct() {
parent::__construct();
$this->post = Container::getModel("Funcionario");
$this->funcaopost = Container::getModel("Funcao");
}
public function index(){
$this->view->funcoes= $this->funcaopost->All();
$this->setPageTitle("Adicionar Usuário");
return $this->renderView('funcionario/index', 'layout');
}
}
class Funcionario extends BaseModel{
protected $table = 'funcionario';
public function rules(){
return [
'nome' => 'required',
'cpf' => 'required',
'usuario' => 'required',
'senha' => 'required',
'funcao_id_funcao' => 'required',
];
}
}
class Funcao extends BaseModel{
protected $table = 'funcao';
public function rules(){
return [
'tipo' => 'required',
'descricao' => 'required',
];
}
}
abstract class BaseController {
protected $view;
private $viewPath;
private $layautPath;
private $pageTitle = null;
public function __construct(){
$this->view = new \stdClass;
}
protected function renderView($viewPath, $layaoutPath = null){
$this->viewPath = $viewPath;
$this->layautPath = $layaoutPath;
if($layaoutPath){
return $this->layout();
} else {
return $this->content();
}
}
protected function content(){
if(file_exists(__DIR__ . "/../App/Views/{$this->viewPath}.phtml")){
return require_once __DIR__ . "/../App/Views/{$this->viewPath}.phtml";
}else{
echo 'Error: View path not found';
}
}
protected function layout(){
if(file_exists(__DIR__ . "/../App/Views/{$this->layautPath}.phtml")){
return require_once __DIR__ . "/../App/Views/{$this->layautPath}.phtml";
}else{
echo 'Error: Layout path not found';
}
}
protected function setPageTitle($pageTitle){
$this->pageTitle = $pageTitle;
}
protected function getPageTitle($separator = null){
if($separator){
return $this->pageTitle . " " . $separator . "";
}else{
return $this->pageTitle;
}
}
}
abstract class BaseModel {
private $pdo;
protected $table;
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
}
class Container {
public static function newController($contoller){
$objContoller = "App\\Controllers\\" . $contoller;
return new $objContoller;
}
public static function getModel($model){
$objModel = "\\App\\Models\\" . $model;
return new $objModel(DataBase::getDataBase());
}
}