0
votes

i was trying to make master Model this error come:

Fatal error: Uncaught TypeError: Argument 1 passed to App\Models\Model::__construct() must be an instance of App\Models\Database, instance of App\Database given, called in C:\xampp\htdocs\Work\AppDarbNajah\App\Controllers\ArticlesController.php on line 14 and defined in C:\xampp\htdocs\Work\AppDarbNajah\App\Models\Model.php:9 Stack trace: #0 C:\xampp\htdocs\Work\AppDarbNajah\App\Controllers\ArticlesController.php(14): App\Models\Model->__construct(Object(App\Database)) #1 C:\xampp\htdocs\Work\AppDarbNajah\Public\index.php(22): App\Controllers\ArticlesController->loadArticles() #2 {main} thrown in C:\xampp\htdocs\Work\AppDarbNajah\App\Models\Model.php on line 9

My code is 1- Model

<?php
namespace App\Models;


class Model{
  protected $db;
  protected $table;

  public function __construct(Database $db){
     $this->db = $db;
  }
  public function query($statement, $attributes= null){
     return $this->db->query($statement);
  }
}   

2- ArticlesController

<?php
namespace App\Controllers;


use App\Models\ArticlesModel;

class ArticlesController{
     public function loadArticles() {

     $app = new \App();
     $art_table = new ArticlesModel($app->getDb());
     return $art_table->load();


  }
}
1
My guess: you want __construct(\App\Database $db) - or put use App\Database; after the namespace (in Model) - Jeff
many thanks for you it's work now - tokhy2000

1 Answers

0
votes

Since the class Model is in namespace App\Models; a (relative) reference to Database would be App\Models\Database in total.

You could now make the reference absolute:

__construct(\App\Database $db) { ...

or put use App\Database; after the namespace.

More info can be found in the Docs