Search code examples
phprequire

can't import PHP class in a regular php file


I am trying to build a son api in native php. I created a structure with classes and now I am trying to use those classes in a regular php file that will get called from outside using an http client.

THe problem is that I can't instantiate new objects from those classes, when I do everything stops and I don't get to continue the execution of my file.

user.php :

use Actions\Users\UserMethods;

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
   try{
       $Methods = new UserMethods();

       if($_GET["id"]!==null){
           $user = $Methods->getUser($_GET["id"]);
           var_dump($user);
           echo json_encode(['user'=>$user]);
       }
   }catch (Exception $exception){
       returnError($exception->getMessage());
   }
}

I can vardump anything before the like where I assign $Methods, but after I do assign nothing works.

UserMethods Class :

namespace Actions\Users;

use Models\UserModel;
use database\dbconnect;
use Constants\DbConfig;

class UserMethods
{

    private $DataBase;

    private $userValidator;

    /**
     * UserMethods constructor.
     */
    public function __construct()
    {

        try{


            $this->userValidator = new UserValidator();

            $this->DataBase = new dbconnect(DbConfig::$BD_HOST.":".DbConfig::$DB_PORT,
                DbConfig::$DB_USERNAME,DbConfig::$DB_PASSWORD,DbConfig::$DB_DBNAME);
        }catch(\Exception $exception){

            var_dump($exception);
        }
    }

    public function createUser(UserModel $userModel){
        try{
            $this->userValidator->validate($userModel);
            $name=$userModel->getName();
            $email= $userModel->getEmail();

            $sqlQuery="Insert into users(email,name) values ($$email,$$name)";
            return $this->handleQuery($sqlQuery);

        }catch(\Exception $exception){
            throw $exception;
        }
    }


    public function updateUser(UserModel $userModel){
        try{
            $this->userValidator->validate($userModel);
            $name=$userModel->getName();
            $email= $userModel->getEmail();
            $id=$userModel->getId();
            $sqlQuery="
                UPDATE users
                SET 
                    name = $$name,
                    email = $$email,
                    ...
                WHERE
                    id=$$id;
            ";
            return $this->handleQuery($sqlQuery);

        }catch(\Exception $exception){
            throw $exception;
        }
    }


    public function deleteUser($id){
        try{
            $sqlQuery="
            DELETE FROM users
            WHERE
                id=$$id;
            ";
            return $this->handleQuery($sqlQuery);

        }catch(\Exception $exception){
            throw $exception;
        }
    }

    public function getUser($id){
        try{

            $sqlQuery="
            SELECT id,name,email
            FROM users
            WHERE id=$$id
            ORDER BY id ASC
            ";
            return $this->handleQuery($sqlQuery);

        }catch(\Exception $exception){
            throw $exception;
        }
    }


    public function getAllUsers(){
        $sqlQuery="
            SELECT id,name,email
            FROM users
            ORDER BY id ASC
            ";
        return $this->handleQuery($sqlQuery);
    }


    private function handleQuery($query){
        $MysqlDb = $this->DataBase->connect();
        return $MysqlDb->query($query);
    }

}

What can be the problem ?


Solution

  • You seem to miss the import of the file. When using namespaces, to make the import almost automatic, it's good to implement an autoloader, I suggest to check how to enable autoloading using composer

    If you have few classes you can avoid using an autoloader and just importing every class manually, in this case you should:

    require UserMethods.php
    

    at the beginning of your User.php file.