8

This is my user class:

<?
require_once(dirname(__FILE__).'/DB.class.php');
require_once(dirname(__FILE__).'/Model.class.php');
class Benutzer extends Model
{
 private $id;
 private $loginName;
 private $loginPassword;
 private $eMail;
 private $rights;
 private $mailchange;
 private $name;
 private $vorname;
 private $matrikelnr;
 private $gruppe;
 /**
 * @TODO PhpDoc
 */
 protected function __construct($id = null)
 {
 if ($id !== null)
 $this->load($id);
 }
 /**
 * @TODO PhpDoc
 */
 public static function factory($id = null)
 {
 if ($id !== null && is_integer($id)){
 return new Benutzer($id);
 } else {
 return new Benutzer();
 }
 }
//getter and setter...
}

This is the abstract class that is inherited by the user class:

<?php
require_once(dirname(__FILE__).'/RowDataGateway.interface.php');
abstract class Model implements RowDataGateway
{
 public function getParameters()
 {
 return get_object_vars($this);
 }
 /**
 * @TODO PhpDoc
 */
 public function setParameters(array $values, $override = true)
 {
 foreach ($values as $key => $val) {
 if ($override){
 if (property_exists(get_class($this), $key))
 $this->$key = $val;
 }
 else {
 if (property_exists(get_class($this), $key) && !isset($this->key))
 $this->$key = $val;
 }
 }
 }
 /**
 * @TODO PhpDoc
 */
 public function load($id, $override = true)
 {
 $res = QueryBuilder::selectQuery($this);
 $result = DB::getInstance()->query($res['query'], array('id' => $id));
 $this->setParameters($result, $override);
 return $this;
 }
 /**
 * @TODO PhpDoc
 */
 public function save()
 {
 if (isset($this->id)){
 $res = QueryBuilder::updateQuery($this);
 if (DB::getInstance()->update($res['query'], $res['params'])){
 return $this;
 }
 }
 else {
 $res = QueryBuilder::insertQuery($this);
 if ($id = DB::getInstance()->insert($res['query'], $res['params'])){
 $this->id = $id;
 return $this;
 }
 }
 return false;
 }
 /**
 * @TODO Implement
 */
 public
 function delete()
 {
 }
}

Everytime i call the $obj->getParameters() function i get an empty array (in the foreach):

 public static function insertQuery(Model $obj)
 {
 //INSER INTO classname (field1,field2,field3) VALUES (val1,val2,val3)
 $vars = null;
 $query = 'INSERT INTO `'.strtolower(get_class($obj)).'` (';
 foreach ($obj->getParameters() as $key => $val) {
 if ($key !== 'id')
 $query .= $key.',';
 }
 $query = substr($query, 0, strlen($query) - 1);
 $query .= ') VALUES (';
 foreach ($obj->getParameters() as $key => $val) {
 if ($key !== 'id'){
 $query .= ':'.$key.',';
 $vars[':'.$key] = $val;
 }
 }
 $query = substr($query, 0, strlen($query) - 1);
 $query .= ')';
 return array('query' => $query, 'params' => $vars);
 }

I've already debugged the code and the right object is given to the function!?

Where is my mistake?

FMK

asked Mar 2, 2012 at 14:26

2 Answers 2

20

That's because the properties of the child class are not visible from the method. You have two options:

  • Move the method to the child class.
  • Turn the visibility of the properties from private into protected.
answered Mar 2, 2012 at 19:39
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that exactly was the mistake!
-1

From PHP 5.3.0, you can also use the ReflectionClass.

$reflect = new \ReflectionClass(get_class($this));
$props = [];
foreach ($reflect->getProperties() as /* @var $prop \ReflectionProperty */ $prop) {
 $prop->setAccessible(true);
 $props[$prop->name] = $prop->getValue($this);
 $prop->setAccessible(false);
}
answered May 26, 2022 at 11:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.