Design Patterns-an example of a PHP combination pattern related issues

Source: Internet
Author: User

  _name = $name; } function Remove (MenuComponent $component) {} function Add (MenuComponent $component) {$this->_item    s[$component->getname ()] = $component;    } function GetName () {return $this->_name;              } public Function displayoperation () {Static $align = ' | ';                  $align. = '; echo $this->_name, "
"; foreach ($this->_items as $name = + $item) {echo $align; $item->displayoperation (); }}}class client{static function DisplayMenu () {$subMenu 1 = new Menucomposite (' submenu1 '); $subMenu 1->add ($subMenu 1); $subMenu 1->displayoperation (); }}client::d isplaymenu ();

The output is an infinite loop
Submenu1
|submenu1
|submenu1
...

Why is this, the first recursive call Displayoperation () after the foreach inside should be empty, stopped, why the infinite loop?

Reply content:


  _name = $name; } function Remove (MenuComponent $component) {} function Add (MenuComponent $component) {$this->_item    s[$component->getname ()] = $component;    } function GetName () {return $this->_name;              } public Function displayoperation () {Static $align = ' | ';                  $align. = '; echo $this->_name, "
"; foreach ($this->_items as $name = + $item) {echo $align; $item->displayoperation (); }}}class client{static function DisplayMenu () {$subMenu 1 = new Menucomposite (' submenu1 '); $subMenu 1->add ($subMenu 1); $subMenu 1->displayoperation (); }}client::d isplaymenu ();

The output is an infinite loop
Submenu1
|submenu1
|submenu1
...

Why is this, the first recursive call Displayoperation () after the foreach inside should be empty, stopped, why the infinite loop?

The implementation of add$this->_items[$component->getName()] = $component;

Note $subMenu1->add($subMenu1);

实例$subMenu1的属性$_items["submenu1"]是他自己本身$subMenu1

And then infinity goes on ...

Class attribute $this->item has been the object you've instantiated, so this displayoperation () method has been circulating.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.