Home arrow PHP arrow Page 3 - Object Interaction in PHP: Introduction to Composition

Composition in a practical sense: a basic example - PHP

Composition is an important concept in PHP. It occurs when an object creates another object; that is, the first object completely possesses the second object. In this article (the first of two parts), Alejandro Gervasio explains composition, and shows some examples to illustrate his points.

  1. Object Interaction in PHP: Introduction to Composition
  2. Composition: some theory ahead
  3. Composition in a practical sense: a basic example
  4. The “Table” class: a simple row-color alternator
  5. Working with the classes: a quick example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 29
July 11, 2005

print this article



As a introduction to composition in PHP, we’ll see a rather basic example that shows how one object composes another one. Since we’re speaking about object interaction, we need at least two objects to work with. So, to properly understand the basics of composition, let’s define two classes that comes handy to clarify any possible question.

The first class that we’ll define for our example is simply a “Page” class, which shows several methods for building a simplistic Web page. Based on the classic page schema composed of  “Header,” “Body” and “Footer” sections, the class presents a specific method to create each one of them. Here’s how our “Pager” class looks:

class Page {
    var $title;
    var $html;
    function Page($title='Default Page'){
function makeHeader($header){
    function makeBody($content=array()){
        return new Table($this,$content);
    function makeFooter($footer){
    function display(){
        return $this->html;

As you can see, the class is very simple and clear to understand. As mentioned before, it presents four methods concerned with building a web page, defined as follows: “makeHeader()”, makeBody()”, “makeFooter()” and finally “display()”, which simply returns the complete HTML markup to be dumped directly to the browser.

The “Page” class contains only two data members, necessary to generate the entire page: $title and $html. As you might guess, the first property will be used to display the page’s title, while the second will store the progressive HTML output, to be finally echoed to the browser. In fact, there is not much to be said about the basic “makeHeader()” and “makeFooter()” methods, since they speak for themselves.

Because of their simplicity, we’ll just say that they take care of creating the header and footer page sections, and add a few regular tags to wrap up the content of each section, which is passed as an argument to every specific method. However, not everything is as boring as seems. If we take a closer look at the “makeBody()” method, we quickly realize that it’s quite special. Why? Well, the method accepts a single parameter, $content that is defined as an array structure, in order to generate the main section of the page

But, it’s doing something else. Notice that it’s returning a new object called “Table”, as we can appreciate in the following line:

return new Table($this,$content);

This looks very interesting. Has it caught your attention? Fine. Here we have a “Page” class which is directly creating an instance of another “Table” object. Given this condition, we can say that the “Table” object composes the “Page” object. Pretty good, right? Just ask yourself the question previously mentioned: what happens to the “Table” object if the “Page” object ever dies? Certainly, it will die too, so it’s not capable of surviving on its own as an independent object. Thus, having answered that question, we have one object that composes another one.

Although the method is very simple, it shows clearly how we’ve defined the interaction process between the two objects. Notice that the newly created “Table” object accepts two parameters for its constructor: the first is the “Page” object itself, referenced as $this, while the second is the $content array used to include some content in the main section of the page.

The reason for creating a new “Table” object answers mostly to the example’s purposes. We want each “Page” object to create a “Table” object, which will render a color-alternated HTML table and display the contents in this container. Definitely, this page generator class isn’t suitable for application in real conditions, but it does its job by showing a clear situation where object composition takes place.

At this point, you’re probably wondering, what does the “Table” class look like? Okay, I thought you’d never ask. So, let’s have a look at its structure. Just click to the next page.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: