需要一次停止脚本

时间:2011-07-18 17:54:28

标签: php oop class require require-once

我有一个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;
    }

}
?>

并且从不打印测试,并且在chrome上页面根本不加载

  

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

我无法想象我的生活。谢谢它提前

1 个答案:

答案 0 :(得分:7)

您的__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);
}


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

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

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