Ph Abd
Ph Abd

Reputation: 77

Access to functions from extended classes

I have a parent class and two classes that are extending it. The question is how to call a function from one extended class that is in other extended class?

code is below:

    // PARENT CLASS
class Controller {
    function __construct() {
        try {
            $this->db = new Database(); // WORKS FINE
        } catch (PDOException $e) {
            die('Database connection could not be established.');
        }
    }
}
class Api extends Controller { 
    function __construct() {
        parent::__construct();
    }
    function RenderForm() { 
        // from here I want to call function that is inside Forms class
    }
}

class Forms extends Controller { 
    function __construct() {
        parent::__construct();
    }
    function ReturnForm() { 
        $this->db->query("SOME QUERY");

        // code here builds a form for output and stores it as $HTML;

        return $HTML;

    }
}

Upvotes: 0

Views: 60

Answers (1)

Shogunivar
Shogunivar

Reputation: 1222

You'll have to make an instance of the forms class like so:

class Api extends Controller { 

    function RenderForm() { 
       $form = new Forms(); //Create instance of your forms class
       $form->ReturnForm(); //Use function from your forms class

    }
}

Upvotes: 2

Related Questions