我想构建'Main'类,其中调用另一个类。
<?php
class Main {
private $admin;
function __constructor(){
spl_autoload_register(array($this, 'autoLoad'));
$this->admin = new AdminFrontEnd();
}
function autoLoad($class) {
include $class.'.php';
}
function debug($s){
echo '<pre>';
print_r($s);
echo '</pre>';
}
function teszt(){
echo $this->admin->kiir('debug content');
}
此代码引发错误:致命错误:在第 28 行的 Classes/Main 中的非对象上调用成员函数 kiir(.php
如何在构造函数中设置更多的类调用? 例如:
function __constructor(){
/* ... */
$this->admin = new AdminFrontEnd();
$this->invoice = new InvoiceGenerate();
$this->pdf = new GeneratePDF();
/* etc ... */
}
function getPDF(){
return $this->pdf->getPDFLocation();
}
/* etc function ... */
谢谢!
错误很明显,我很惭愧我没有早点发现它......
function __constructor(){...}
应该是:
function __construct(){...}
进行此修改,它将起作用。
另外:如果您需要它:
function __destruct(){..} //not __destructor()
当心!