实例类内部构造

时间:2013-11-11 00:57:50

标签: php oop

我需要在我的控制器类之上实例化2个或更多类,所以我可以在我的控制器类的任何方法中使用$this->filter$this->logger_instance。现在它不让我,我得到一个错误。 (如果可能的话,我不想扩展类。)如果可能的话,可以在构造中实例化。

  

解析错误:语法错误,controller.php中的意外T_NEW

(我正在将编码习惯从程序转移到OOP,所以我真的很擅长。)

class ID_Controller
{
    public $input;
    public $register_attempt = 2;
    public $maximum_attempts = 3;
    public $log_data_attempts = 2;
    public $query_log_file_path;
    public $sql_filtering = true;
    public $xss_filtering = true;
    public $sql_register_attempt = 3;
    public $xss_register_attempt = 6;

    public $filter = new ID_Algorithm;
    public $logger_instance = new ID_Logger;

    function __construct()
    {
    }
}

1 个答案:

答案 0 :(得分:1)

为什么不尝试通过__construct()方法初始化这些类?

/*
    If the two classes are located in seperate files, be sure to require them:
*/
require("ID_Algorithm Page");
require("ID_Lodder Page");
class ID_Controller {
    /* previous lines here */ 
    /* 
    Comment out the next two lines and initiate them within the construct class 
    */
    // public $filer = new ID_Algorithm;
    // public $logger_instance; 
    public $filer;
    public $logger_instance
    public function __construct(){
        $this->filter = new ID_Algorithm;
        $this->logger_instance = new ID_Logger;
    }
}

然后致电:

$Class = new ID_Controller();

这将正确设置必要的内部指针。