私はそれについて多くの投稿を見てきましたが、本当の答えはありません:(。私の問題は次のとおりです:
次の 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);
}
}
?>
私の index.php では、2 つの Bean をインスタンス化します。
$peopleBean = new ResultBean("Participants");
$countryBean = new ResultBean("Countries");
そして、それらを次のように使用したいと思います:
$beanArray[0] = $peopleBean;
$beanArray[1] = $countryBean;
for($i = 0; $i < 2; $i++){
$resultArray = array();
$bean = $beanArray[$i];
echo "bean label :" . $bean->label . " <br/>";
etc.
ページの読み込み時に次のエラー メッセージが表示されます。
致命的なエラー: メッセージ「ResultBean クラスの読み取りプロパティ getLabel が無効です」を含む例外「Exception」をキャッチできませんでした。パス/resultBean.php:103内
スタック トレース: #0パス/resultBean.php(106): ResultBean->__get('getLabel') #1パス/index.php(123): ResultBean->__get('label') #2 {main} がスローされる103 行目のpath /resultBean.php
私が読んだすべてのページから、Java スタイルの class-casting を使用することはできません$bean = (ResultBean) $beanArray[$i]
。
どうすればこれを修正できますか? ありがとうございます=)
注: __autoload() 関数のオーバーライドを使用してクラスをロードすると、正常に動作します。
注2:私は(配列の外で)呼び出しを行いますが$peopleBean->numRows = *smthing*
、それもうまくいくようです。