gpt4 book ai didi

抽象类中的php get_object_vars

转载 作者:可可西里 更新时间:2023-11-01 00:00:58 25 4
gpt4 key购买 nike

这是我的用户类:

<?
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...
}

这是用户类继承的抽象类:

<?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()
{
}
}

每次我调用 $obj->getParameters() 函数时,我都会得到一个空数组(在 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);
}

我已经调试了代码并且正确的对象被赋予了函数!?

我的错误在哪里?

我的世界

最佳答案

那是因为子类的属性在方法中是不可见的。您有两个选择:

  • 将方法移至子类。
  • 将属性的可见性从私有(private)变为 protected 。

关于抽象类中的php get_object_vars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9534877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com