Jump to content

picokojama

Recommended Posts

Hi everyone,

 

I have something like more advanced problem, so I hope someone will help.

No need for looking for variables and functions matches, I have good editor, and if there is any, it is because I am translating code to english.

 

 

Here I have class DBobject, which contains all common database methods, but my focus is on two:

 

public static function find_by_sql($sql) {        global $pdo; // pdo connection works fine        $query = $pdo->query($sql);        $object_array = array();        while($row = $query->fetchAll()) {            $object_array[] = static::instantiate($row);        }        return $object_array;    }    public static function instantiate($row) {        $class = get_called_class();        $object = new $class; // because it can be used for users, pages... I don't wont to repeat my code            foreach ($row as $attribute => $value) {                $object->$attribute = $value;            }        return $object;    }

It is parent class of many classes, and I am looking in the class Comments

protected static $table_name='komentari';protected static $db_fields=array(// all of my fields are here);public $id;public $user_id;// and all of database fields as variables
And now, when I want to call it...
$comment = Comment::find_by_sql(// here I select user based upon id); // this should return object with attributes from my database (because I returned object in instantiate function), am I right?echo $comment->content; // I exept here to echo lorem ipsum dolor sit amet....

And I get error that I called attribute of non-object $comment, aldo I returned the object in instantiate method

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...