Jump to content

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...