Problem with class extends in PHP

2

I have a problem with a class, in which I extend the Conn class which is my database connection. The error is

  

Class 'Conn' not found in ...

<?php 

class User extends Conn{

    private $nombre;
    private $username;

    public function ValidarExistencia($user, $password){
        $md5pass = md5($password);

        $sql = "SELECT * FROM usuario WHERE username = :user AND password = :password";
        $query = $this->connect()->prepare($sql);
        $query->execute(['user' => $user, 'password' => $md5pass]);

        if($query->fetch(PDO::FETCH_ASSOC)){
            return true;
        }else{
            return false;
        }
    }

    public function SetUser($user){
        $sql = "SELECT * FROM usuario WHERE username = :user";
        $query = $this->connect()->prepare($sql);
        $query->execute(['user' => $user]);

        foreach ($query as $currentUser) {
            $this->nombre = $currentUser['nombre'];
            $this->username = $currentUser['username'];
        }
    }

    public function getNombre(){
        return $this->nombre;
    }

}

?>

Conn class code:

<?php

class Conn
{

    //Atributos de la base de datos
    private $dbname;
    private $host;
    private $user;
    private $pass;
    private $port;
    private $conexion;

    //Métodos
    public function __construct()
    {
        $this->dbname = "scrum";
        $this->host = "localhost";
        $this->user = "postgres";
        $this->pass = "1234";
        $this->port = "5432";
    }

    public function connect(){
        try{
        $this->conexion = new PDO("pgsql:host=".$this->host.
                            ";port=".$this->port.
                            ";dbname=".$this->dbname.
                            ";user=".$this->user.
                            ";password=".$this->pass);
        }catch(Exception $e)
        {
            echo "Tienes el siguiente error:", $e->getMessage();
        }
    }

}

?>
    
asked by Cristian Camilo Cadavid Escarr 25.11.2018 в 15:30
source

1 answer

2

To be able to use the properties and methods of the class Conn in the file User apart from doing the inheritance process with:

class User extends Conn

You must also request the file in this way

If both are in the same folder, then it should be like this:

require 'Conn.php';

class User extends Conn
{
  .....
}
  

Done the previous thing, at the time of doing the process of inheritance, the   class will be found; because this file was already loaded before

Keep the following in mind:

  
  • require and require_once are similar and will result in a fatal error when the invoked file is not localized; which leads to the interruption of the script
  •   
  • The use of include e include_once obeys sigurán loading the content of the rest of the page but there will be a warning that   something wrong when wanting to upload a file
  •   
        
    answered by 25.11.2018 / 15:35
    source