从扩展类访问函数

时间:2017-03-31 11:31:09

标签: php class

我有一个父类和两个扩展它的类。问题是如何从其他扩展类中的一个扩展类调用函数?

代码如下:

    // 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;

    }
}

1 个答案:

答案 0 :(得分:2)

你必须像forms那样创建一个class Api extends Controller { function RenderForm() { $form = new Forms(); //Create instance of your forms class $form->ReturnForm(); //Use function from your forms class } } 类的实例:

       $(document).ready(function () {
          $('#PopupDiv').hide();
          $('#Btn1').click(function () {
             alert()
             $('#PopupDiv').show();
          })
          $(document).click(function () {
             alert('o')
             if ($('#PopupDiv').is(':visible')) {
                alert('vis');
             }
          })
       })