-1

我需要在我的控制器类之上实例化 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()
    {
    }
}
4

1 回答 1

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();

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

于 2013-11-11T01:06:25.300 回答