2

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

willium
  • 2,048
  • 5
  • 25
  • 34
  • 1
    Try adding error_reporting(E_ALL); to the top of your script. Do you get any new error messages? – Nick Jul 18 '11 at 17:56
  • View source in Chrome. For short error messages, Chrome hides yours and shows their default one. Annoying "feature". – ceejayoz Jul 18 '11 at 17:56
  • 1
    @ceejayoz HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request. and error reporting didn't show anything new. – willium Jul 18 '11 at 17:58

1 Answers1

7

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 :

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
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • you are a god. This has been pissing me off for the most of the weekend. I knew it was simple, and now I feel like a disgrace for calling myself a php developer. :) Keep up the good work. – willium Jul 18 '11 at 18:01
  • Next time *(well, actually, all the time, when developping ;-) )* enable error reporting ;-) – Pascal MARTIN Jul 18 '11 at 18:02
  • appreciate it, and will do. :) – willium Jul 18 '11 at 19:26
  • 1
    I can't get error reporting working, Putting stuff like E_ALL reporting at the top of pages does nothing. – willium Jul 21 '11 at 20:22