Require Once停止脚本


Require Once stops script

我有一个php脚本test.php它的内容是

<?php
     require_once("classes/user.php");
     echo "test";
?>
下面是user.php 的内容
<?php 
class User {
    private $data = array();
    public function __set($name, $value) {
        $this->data[$name] = $value;
    }
    public function __get($name) {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }
    public function __isset($name) {
        return isset($this->data[$name]);
    }
    public function __unset($name) {
        unset($this->data[$name]);
    }
    public __construct($param) {
        if(is_array($param)) $this->create($param);
        else $this->id($param);
    }
    private id($id) { //select from database
        require_once('config.php');
        $pdo = new PDOConfig();
        $sql = "SELECT * FROM users WHERE `id` = :id";
        $q = $pdo->prepare($sql);
        $q->execute(array(":id"=>$id));
        $resp = $q->fetchAll();
        foreach ($resp as $row) {
            foreach ($row as $key=>$value) {
                if(!is_int($key))
                    $this->data[$key] = html_entity_decode($value, ENT_QUOTES);
            }
        }
        $pdo = null;
        unset($pdo);
    }
    private create($arr) { //create new item from values in array and insert to db
    }
    public delete() {
        $this->life = 0;
        //update database "life" here
    }   
    /* ##################################### */
    /* !Functions                            */
    /* ##################################### */
    public projects($extra = null) {
        $projects = array();
        require_once('project.php');
        $pdo = new PDOConfig();
        $sql = "SELECT * FROM ---- WHERE `000` = :aaa";
        if($extra) $sql .= " " . $extra;
        $q = $pdo->prepare($sql);
        $q->execute(array(":aaa"=>$this->id));
        $resp = $q->fetchAll();
        foreach ($resp as $row) {
                $project = new Project($row['id']);
                $projects[] = $project;
                $project = null;
                unset($project);
        }
        return $projects;
    }
}
?>

和test永远不会打印,并且在chrome上页面根本不加载

网站在检索http://example.com/test.php时遇到错误。它可能因维护或配置不正确而关闭。

我怎么也想不明白这个问题。Thanks it advance

您的__construct()方法声明中有语法错误:

public __construct($param) {
    if(is_array($param)) $this->create($param);
    else $this->id($param);
}

您需要使用function关键字,如下所示:

public function __construct($param) {
    if(is_array($param)) $this->create($param);
    else $this->id($param);
}


为了帮助找到这些错误,您应该在开发计算机上启用:

  • error_reporting
  • display_errors

事实上,我只是将您的代码复制粘贴到.php文件并运行它-并得到一个漂亮的

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE 
    in /home/.../temp/temp.php on line 39