Search code examples
phpobjectparent-childaddchild

Adding elements to other elements


I have been using .net for the past couple of years, and I like the way you can add controls at any point in the page from anywhere. For example, you can say Head.Controls.add(new LiteralControl("<link rel='stylesheet' type='text/css' href='styles.css' />")) even if there is already a body.

Is it possible to do this kind of thing in php? My site is set up so that we have agents, customers and artists (this is for a card manufacturing company who wants a customer application for use offline at tradeshows). The add agent form may have different styles to the add customer form. I want to therefore have each type in a different folder (for example agents, customers and artists) each with their own stylesheet. There will be one form page which takes GET parameters of type (artist, customer etc), mode (create, edit) and an optional parameter of ID (when in edit mode). I would like to be able to call $agentForm->generateForm() and $agentForm->generateStyleTag() in one go, rather than what I am currently doing which is to call $agentForm->generateForm() in the body and $agentForm->generateStyleTag when in the head (without even the start body tag being generated yet).

A good way to put this is that I have a Head tag and a Body tag. In another function called $agentForm->generateHTML() I want to say Body->addChild("bla") and Head->addChild("bla"). This makes developing a new page a lot easier since it ensures the styles are there and are correct for the section of the site the user is in.

Is it possible to achieve this, or is this one of the major differences between php and .net?


Solution

  • Have a look at the PRADO framework and the Yii framework. They have a hierarchical, component-based philosphy that allows nesting and composition, essentialling building a whole site from smaller, pluggable components.