Добрый вечер! Подскажите правильно ли я происходит обращение к объекту через $var класса Page (запись в теле класса Polyage $var). И еще как срабатывает запись: $poly->get_ob($page); $poly->get_ob($index); $poly->get_result();. Вывод массива "ob" понятен, но предыдущие две записи ф-ии "get_ob" не пойму почему должны помещать в массив "ob" две записи, а не перезаписывать одна другую? Код (Text): <?php class Page { public $title; public $content; public $footer; public function __construct($t,$c,$f) { $this->title = $t; $this->content = $c; $this->footer = $f; } public function render_body() { $str = '<h1>'.$this->title.'</h1>'; $str .= '<p>'.$this->content."</p>"; $str .= "<p style='font-size=8px'>".$this->footer."</p>"; return $str; } public function test() { return $this->render_body(); } } class Index extends Page { public $slide; public $news; public function __construct($t,$c,$f,$s,$n) { parent::__construct($t,$c,$f); $this->slide = $s; $this->news = $n; } public function render_body() { $str = parent::render_body(); $str.="<p>".$this->slide."</p>"; $str.="<p>".$this->news."</p>"; return $str; } } class Poly { public $ob; public function get_ob(Page $var) { $this->ob[] = $var; } public function get_result() { foreach($this->ob as $item) { echo $item->test(); } } } $poly = new Poly(); $page = new Page('Page','hello i am page','footer'); echo $page->render_body(); $index = new Index ('INDEX','HELLO I am index','footer','slide_show','news'); echo $index->render_body(); $poly->get_ob($page); $poly->get_ob($index); $poly->get_result(); ?>
Код (PHP): public function get_ob(Page $var) { НЕ надо писать Page, просто $var т.е. Код (PHP): public function get_ob($var) { Добавлено спустя 7 минут 13 секунд: потому что при записи: Код (PHP): $this->ob[] = $var; добавляемый элемент записывается под новым индексом в массив (т.е. добавляется в конец массива): Код (PHP): $data = array('a', 'b', 'c'); for ( $i = 0; $i < 5; $i++ ) { $data[] = $i; } // $data будет массив 'a', 'b', 'c', '0', '1', '2', '3', '4' foreach ($data as $v) { echo "<p>{$v}</p>"; }