lundi 24 août 2020

Cannot create new class in PHP? [duplicate]

I am trying to do my first PHP project, I have problems creating new classes, please check for me.

This is my PDO class, I succeeded in querying data at coreModel() (Comment section)

require ("config.php");

class Model
{
    private $host;
    private $username;
    private $password;
    private $dbname;
    private $connect;

    public function __construct()
    {
        $this->host = DB_HOST;
        $this->username = DB_USERNAME;
        $this->password = DB_PASSWORD;
        $this->dbname = DB_NAME;
    }

    public function coreModel(){
        try {
            $this->connect = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
//            $smtm = $this->connect->prepare("SELECT * FROM admin WHERE email = :email AND password = :password");
//            $smtm->setFetchMode(PDO::FETCH_ASSOC);
//            $smtm->execute(['email' => 'admin@aaa.biz', 'password' => md5('12345678')]);
//            echo $smtm->rowCount();
//            foreach ($smtm as $value){
//                echo $value['email'];
//                echo $value['password'];
//            }
            return $this->connect;
        } catch (PDOException $e){
            echo $e->getMessage();
            return false;
        }
    }
}

This is my Model Admin, I still do the query successfully here

require_once ('./Core/Model.php');
class AdminModel
{
    public $model;

    public function __construct()
    {
        $PDOModel = new Model();
        $this->model = $PDOModel->coreModel();
//            $smtm = $this->model->prepare("SELECT * FROM admin WHERE email = :email AND password = :password");
//            $smtm->setFetchMode(PDO::FETCH_ASSOC);
//            $smtm->execute(['email' => 'admin@aaa.biz', 'password' => md5('12345678')]);
//            echo $smtm->rowCount();
//            foreach ($smtm as $value){
//                echo $value['email'];
//                echo $value['password'];
//            }
//            echo 1;
    }
    public function hello(){
        echo 'hi';
    }
}

My last problem lies here, when creating a new AdminModel there is an HTTP ERROR 500 error, but if I echo in the constructor of AdminModel what I see is the echo content. When I call the method hello() the error message remains the same.

require_once ("./Models/AdminModel.php");

class LoginAdminController
{
    public $modelAdmin;

    public function index(){
        $this->modelAdmin =  new AdminModel();
//      $this->modelAdmin->hello();
        $path = Helpers::getPathView('admin').'login.php';
        require "$path";
    }
}

All require is correct, I have checked. Thanks for the help




Aucun commentaire:

Enregistrer un commentaire