Page 1 of 1

Problem when returning an object array. Please help

Posted: Thu Nov 05, 2009 10:28 pm
by su93rn0va
Ok this is my class. The red part is where i get the fatal error saying "Cannot use [] for reading in C:\xampp\... on line 29." I have also put underneeth the class my index.php. I am new to object oriented PHP and I can't figure this out. Somebody help...

Code: Select all

<?php
require_once("database.php");
 
class User{
    public $id;
    public $username;
    public $password;
    public $first_name;
    public $last_name;
 
    public static function find_all(){
    return self::find_by_sql("select * from users");
     
    }
    
    public static function find_by_id($id=0){
    global $database;
    $result_array = self::find_by_sql("select * from users where id = {$id} LIMIT 1");
    return !empty($result_array) ? array_shift($result_array) : false;
    }
    
    public static function find_by_sql($sql=""){
    global $database;
    $result_set = $database->query($sql);
    $object_array = array();
    while ($row = $database->fetch_array($result_set)){
        $object_array[] = self::instantiate($row);
        }
[color=#BF0000] return $object_array[];[/color]
    }
 
    private static function instantiate($record){
    // could check that $record exists and is an array
    $object = new self;
    //$object->id           = $record['id'];
    //$object->username = $record['username'];
    //$object->password = $record['password'];
    //$object->first_name = $record['first_name'];
    //$object->last_name  = $record['last_name'];
    foreach($record as $attribute=>$value){
    if($object->has_attribute($attribute)){
        $object->attribute = $value;
            }
    }
    return $object;
}
    
    private function has_attribute($attribute){
    //get_object_vars returns an assosiative array with all attributes
    // (includes private ones!) as the keys and their current values as the value 
    $object_vars = get_object_vars($this);
    // we don't care about the value, we just want to know if the key exists
    // Will return true or false
    return array_key_exists($attribute , $object_vars);
    }
 
}
 
THIS IS MY index.php

Code: Select all

 
<?php 
require_once("../includes/config.php"); 
require_once("../includes/database.php");
require_once ("../includes/user.php");
 
 
echo "<hr />";
 
$user = User::find_by_id(1);
echo $user->first_name . "<br />";
 
$users = User::find_all();
foreach ($users as $user) {
 echo "User : ". $user->username ."<br />";
 
 
}
?>

Re: Problem when returning an object array. Please help

Posted: Thu Nov 05, 2009 10:37 pm
by requinix
You simply can't do that. Considering what [] means it doesn't make sense.

Code: Select all

return $object_array;

Re: Problem when returning an object array. Please help

Posted: Thu Nov 05, 2009 10:54 pm
by su93rn0va
Thanks a lot for the fast answer... I had also the same idea but still doesn't work.

Code: Select all

return $object_array;
When I do it the above way don't get any output on the browser from my database! id=1 record exists in the users table.
But what is does really mean this error Cannot use [] for reading ? I don't try to read, I am just returning the array. I am very confused :banghead: Thanks again for the reply

Re: Problem when returning an object array. Please help

Posted: Fri Nov 06, 2009 4:26 am
by jackpf
[] is for writing to an array. You are reading it. Try using var_dump() or print_r() to see what it contains.