Search code examples
phpclassextend

PHP class extend


I have a problem understand how class extension work..

I'm trying to extend a class to split functions in different files to have it more organized..

But i have problems accessing variables and function of the main class into child class.

That is whats i have:

Parent class: it is the uFlex Class v 0.88 I do not write it all, because it is long..

class uFlex {
    //Constants
    const version = 0.88;
    const salt = "";
    //End of constants\\\\
    /**
     * PDO / database credentials
     */
    var $db = array(
        "host" => '',
        "user" => '',
        "pass" => '',
        "name" => '',   //Database name
        "dsn" => '' //Alterntive PDO DSN string
    );

        function connect(){
        if(is_object($this->db)) return true;

        /* Connect to an ODBC database using driver invocation */
        $user = $this->db['user'];
        $pass = $this->db['pass'];
        $host = $this->db['host'];
        $name = $this->db['name'];
        $dsn = $this->db['dsn'];

        if(!$dsn){
            $dsn = "mysql:dbname={$name};host={$host}";
        }

        $this->report("Connecting to database...");

        try{
            $this->db = new PDO($dsn, $user, $pass);
            $this->report("Connected to database.");
        }catch(PDOException $e){
            $this->error("Failed to connect to database, [SQLSTATE] " . $e->getCode());
        }

        if(is_object($this->db)) return true;
        return false;
    }
}

Then:

<?php
class admin extends uFlex {

    function adm_getUsers(){
            if(!$this->connect()) return false;

            $sql= "SELECT * from users LIMIT 30";
            $st = $this->db->prepare($sql);
        $out = $st->execute();
            $row = $st->fetchAll(PDO::FETCH_ASSOC);
            return $row;    
    }

    function adm_getSingleUser($id){
            if(!$this->connect()) return false;
        if(is_numeric($id)){
            $sql= "SELECT * from users WHERE id = '$id'";
            }else{
            $sql= "SELECT * from users WHERE username = '$id'";
            }
            $st = $this->db->prepare($sql);
        $out = $st->execute();
            $row = $st->fetch(PDO::FETCH_ASSOC);
            return $row;
    }
}

?>

I intiialize them in a config file i include in every page:

$user = new uFlex(false);
$admin = new admin();

But when using $admin->adm_getUsers(); $row array it's empty.

Before to try to split the functions between 2 classes, i was using the same function on the main class, and was working.

It is the first time i try to extend a class.. i searched on google, and also readed some question here, but it is just too complicated for me to understand, since i'm still learning PHP.


Solution

  • This is where inheritance is not really the best answer. Instead you can drop the inheritance and use composition instead. Pass your instance of uFlex through as a dependency of Admin as follows:

    $user = new uFlex(false);
    $admin = new Admin($user); // uFlex is being passed in
    

    You will first need to update your PHP class as there are a couple of changes:

    class Admin {
    
        // Added private variable that will hold the uFlex instance
        private $user;
    
        // Added a class constructor which will be called when we create a new Admin
        function __construct($user) { // Receives an instance of uFlex
            $this->user = $user;
        }
    
        function adm_getUsers(){
            if(!$this->user->connect()) return false; // Call connect on user
            $sql= "SELECT * from users LIMIT 30";
            $st = $this->user->db->prepare($sql); // Call prepare on db of user
            $out = $st->execute();
            $row = $st->fetchAll(PDO::FETCH_ASSOC);
            return $row;    
        }
    
        function adm_getSingleUser($id){
            if(!$this->user->connect()) return false; // Same here
            if(is_numeric($id)) {
                $sql= "SELECT * from users WHERE id = '$id'";
            } else {
                $sql= "SELECT * from users WHERE username = '$id'";
            }
            $st = $this->user->db->prepare($sql); // And here
            $out = $st->execute();
            $row = $st->fetch(PDO::FETCH_ASSOC);
            return $row;
        }
    }