我如何在類B函數C中運行方法$ this-> ob-> getVar()?我不知道。我是否必須將字符串傳遞給構造函數?A類和B類之間的傳輸變量
<?php
class A{
public $tabb = array('1'=>'one', '2'=>'two');
public $index;
public function setVar($v){
$this->index = $v;
}
public function getVar(){
return $this->index;
}
public function arr(){
return $this->tabb;
}
}
class B{
public $tab;
public function __construct($var){
$this->ob=new A;
$this->tab = $var;
}
public function C(){
return $this->D($this->tab, $this->ob->getVar());
}
public function D($l, $j){
if(is_array($l) && isset($j)){
print 'yes';
} else {
print 'no';
}
}
}
$obb = new A;
$obb->setVar('onetwo');
$k = $obb->arr();
$obbb = new B($k);
$obbb->C();
?>
只需在該方法中調用'$ this-> ob-> getVar()'。這段代碼是完全有效的 – zerkms
相同的效果只有數組傳輸的構造函數 – xyz