开发者

Require Once stops script

开发者 https://www.devze.com 2023-03-21 17:14 出处:网络
I have a php script, test.php that has the contents <?php require_once(\"classes/user.php\"); echo \"test\";

I have a php script, test.php that has the contents

<?php
     require_once("classes/user.php");
     echo "test";
?>

and here is the contents of 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;
    }

}
?>

and test is never printed, and on chrome the page doesn't load at all

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

I can't figure this out for the life of me. Thanks it advance


You have a syntax error in the declaration of your __construct() method :

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

You need to use the function keyword, like this :

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


To help find those errors, on your development computer, you should enable :

  • error_reporting
  • and display_errors

In fact, I just copy-pasted your code to a .php file and ran it -- and got a nice

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号