php - 需要一次停止脚本

标签 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 上页面根本不会加载

The website encountered an error while retrieving http://example.com/test.php. It may be down for maintenance or configured incorrectly.

我这辈子都想不通。提前谢谢

最佳答案

__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

关于php - 需要一次停止脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737124/

相关文章:

php - 支持模糊搜索的最容易实现的站点搜索应用程序是什么?

php - 在表格中查找相似的数字模式

c++ - 使用模板在类(双向链表)中定义结构

c++ - 遍历 txt 文件中的对象数组

class - 尝试通过 scikit-learn 中的 sample_weight 平衡我的数据集

php - 使用 PHP MySQL .htaccess 和 GET 重写 URL

php - Yii2:在 ActiveRecord 的 with() 中使用 realtion 的范围

ios - 如何跨多个 View Controller 更新类的单个实例?

Javascript OOP - 通过接口(interface)或原型(prototype)验证对象时的最佳实践

javascript - 我应该如何将 OOP 模式实现到交互式 Web 应用程序(借助 jQuery)?