Er det noen som er god på objektorientert PHP og som kan si meg hva som jeg gjør feil her. Hvorfor får jeg ingen utskrift når jeg kjører koden?
Her er kode:
Her er klasser:
Her er kode:
PHP:
$c = new C();
$c->addB();
$c->addA(0);
$c->addAContent(0, 0, 'test');
$c->addB();
$c->addA(1);
$c->addAContent(0, 1, 'test2');
echo $c->getString();
Her er klasser:
PHP:
class C {
private $cnr;
private $c;
public function __construct() {
$this->c = array();
$this->cnr = 0;
}
public function addB() {
$this->b[$this->bnr++] = new B();
}
public function addA($bnr) {
$this->b[$bnr]->addA();
}
public function addAContent($bnr, $anr, $content) {
$this->b[$bnr]->addAContent($anr, $content);
}
public function getString() {
$data = '';
for ($i = 0; $i < $this->bnr; $i++) {
$text = $this->b[$i]->getString();
$data .= $text;
}
$x = new X($data);
return $x->getString();
}
}
class B {
private $anr;
private $a;
public function __construct() {
$this->anr = 0;
$this->a = array();
}
public function addA() {
$this->a[$this->anr++] = new A();
}
public function addAContent($anr, $content) {
$this->a[$anr]->addContent($content);
}
public function getString() {
$data = '';
for ($i = 0; $i < $this->anr; $i++) {
$data .= $this->a[$i]->getString();
}
$x = new X($data);
return $x->getString();
}
}
class A{
private $content;
public function __construct() {
$this->content = '';
}
public function addContent($content) {
$this->content = $content;
}
public function getString() {
$x = new X($this->content);
return $x->getString();
}
}
class X {
private $text;
public function __construct($text) {
$this->text = $text;
}
public function getString() {
return $this->text;
}
}