PHP:从数组中的对象调用函数

Flo*_*lin 0 php casting

我有很多关于它的帖子,但没有真正的答案:(.我的问题如下:

我有以下Bean类:

<?php
class ResultBean {


private $_label;
private $_resultSet;
private $_headerSet;
private $_numRows = 0;
private $_numFields = 0;
private $_empty = TRUE;
private $_dataArray;
private $_headerArray;

public function __construct($label) {
    $v = trim($label);
    $this->_label = empty($label) ? "" : $v;

}

public function setResultSet($value) {

    if(!$value){
        $this->_resultSet = null;
        $this->_empty = TRUE;
    }else{
        $this->_resultSet = $value;
        $this->_empty = FALSE;
    }

    return $this;
}

public function getResultSet() {
    return $this->_resultSet;
}

public function getLabel() {
    return $this->_label;
}

public function setLabel($value) {
    $v = trim($value);
    $this->_label = empty($value) ? null : $v;
    return $this;

}

public function setHeaderSet($value) {
    $this->_headerSet = !$value ? null : $value;
    return $this;   
}

public function getHeaderSet() {
    return $this->_headerSet;
}

public function setNumRows($value) {
    $this->_numRows = $value;
    return $this;
}

public function getNumRows() {
    return $this->_numRows;
}

public function setNumFields($value) {
    $this->_numFields = $value;
    return $this;
}

public function getNumFields() {
    return $this->_numFields;
}

public function setDataArray($value) {
    $this->_dataArray = $value;
    return $this;
}

public function getDataArray() {
    return $this->_dataArray;
}

public function setHeaderArray($value) {
    $this->_headerArray = $value;
    return $this;   
}

public function getHeaderArray() {
    return $this->_headerArray;
}


public function getEmpty() {
    return $this->_empty;
}

public function __get($propertyName) {
    $method = 'get' . ucfirst($propertyName);
    if (!method_exists($this, $method)) {
        $method = 'is' . ucfirst($propertyName);
        if(!method_exists($this, $method)) {
            throw new Exception('Invalid read property ' . $propertyName . ' in ' . get_class($this) . ' class.');
        }
    }
    return $this->$method;
}

public function __isset($propertyName) {
    try {
        $_value = $this->__get($propertyName);
        return !empty($_value);
    } catch (Exception $e) {
        /* if a property isn't readable it isn't set*/
        return false;
    }
}

public function __set($propertyName, $value) {
    $method = 'set' . ucfirst($propertyName);
    if ('mapper' == $method || !method_exists($this, $method)) {
        throw new Exception('Invalid write property ' . $propertyName . ' in ' . get_class($this) . ' class.');
    }
    return $this->$method($value);
}



}
?>
Run Code Online (Sandbox Code Playgroud)

在我的index.php中,我实例化了两个bean:

$peopleBean = new ResultBean("Participants");
$countryBean = new ResultBean("Countries");
Run Code Online (Sandbox Code Playgroud)

并希望使用它们如下:

$beanArray[0] = $peopleBean;
$beanArray[1] = $countryBean;


for($i = 0; $i < 2; $i++){

    $resultArray = array();
    $bean = $beanArray[$i];

    echo "bean label :" . $bean->label . " <br/>";
    etc.
Run Code Online (Sandbox Code Playgroud)

加载页面时收到以下错误消息:

致命错误:未捕获的异常'Exception',消息'ResultBean类中的读取属性getLabel无效.' 在路径 /resultBean.php:103

堆栈跟踪:#0 路径 /resultBean.php(106):ResultBean - > __ get('getLabel')#1 路径 /index.php(123):ResultBean - > __ get('label')#2 {main}抛出路径 /resultBean.php在第103行

从我读过的所有页面中,都不可能使用java风格的类转换$bean = (ResultBean) $beanArray[$i].

我怎样才能解决这个问题 ?谢谢=)

注意:我使用__autoload()函数的覆盖加载类,它工作正常.

注2:我确实调用(在数组之外)$peopleBean->numRows = *smthing*,它似乎也有效.

Jon*_*Jon 6

你的__get实现是错误的.

而不是这个:

return $this->$method;
Run Code Online (Sandbox Code Playgroud)

你要这个:

return $this->$method();
Run Code Online (Sandbox Code Playgroud)

或这个:

return call_user_func(array($this, $method));
Run Code Online (Sandbox Code Playgroud)

就目前而言,代码正确地检测到您有一个getLabel方法但是它尝试读取getLabel属性,因此__get再次调用,找不到getGetLabel方法并抛出.

另外两个提示:

  1. foreach而不是for.
  2. 你不需要在PHP中将对象强制转换为任何东西; 只需调用方法.