PHP for ASP.NET Developers (part 5 – Object lifetime)

Following from my last post on the basics of object orientation in PHP, what about object lifetimes. How long does an object hang around for?

Class Diagram

In PHP you have a __destruct method on a class. This is like the finaliser in C#. For example, here is a HomoSapien class (a natural extension from the object model above).

class HomoSapien extends Mammal {

    public function __construct()
    {
        echo("Hello World!");
    }

    public function __destruct()
    {
        echo("Goodbye, cruel world!");
    }

    public function Vocalise()
    {
        echo "To be, or not to be...";
    }
}

When an object is create the __construct function is run (as we saw in the last post). The __destruct function is run when the object is unset, like this:

$human = new HomoSapien();
echo("n");
unset($human);
echo("n");

The output shows that the __constuct and __destruct methods were run.

Like the __construct function, the __destruct function does not automatically call the parent (as it would in C#). You have to do that explicitly with code like this:

public function __destruct()
{
    parent::__destruct();
}

Similarly, when the object goes out of scope the __destruct method is run:

function doStuff()
{
    $human = new HomoSapien();
    echo("n");
    $human->Vocalise();
    echo("n");
}

doStuff();

outputs:

Hello World!
To be, or not to be...
Goodbye, cruel world!

The destructor will also be run at the end of a script, so if the objects have not yet gone out of scope by that point they will be run. Any statements that output to the page will be run after the page has rendered. For example, the following script:

<html>
 <head><title>My little test PHP script</title></head>
 <body>
  <pre>
<?php

include_once('HomoSapien.php');

$theMother = new HomoSapien("Caitlin", null, null);
$theFather = new HomoSapien("Iain", null, null);

$theChild = new HomoSapien("Douglas", $theMother, $theFather);

?>
</pre></body></html>

Outputs the following:

<html>
    <head><title>My little test PHP script</title></head>
    <body>
  <pre>
Hello, I'm Caitlin!
Hello, I'm Iain!
Hello, I'm Douglas!
</pre></body></html>Douglas is no longer in the building!
Iain is no longer in the building!
Caitlin is no longer in the building!

And for completeness, here is the HomoSapien.php mentioned in the last example:

<?php
include_once("Mammal.php");

class HomoSapien extends Mammal {
    private $name;

    public function __construct($name, $mother, $father)
    {
        parent::__construct($mother, $father);
        $this->name = $name;
        echo("Hello, I'm {$this->name}!n");
    }

    public function __destruct()
    {
        echo("{$this->name} is no longer in the building!n");
    }

    public function Vocalise()
    {
        echo "I think, therefore I am.n";
    }
}
?>

Mammal.php:

<?php

include_once('Animal.php');

abstract class Mammal extends Animal {
    protected $mother;
    protected $father;

    public function __construct($mother, $father)
    {
        $this->mother = $mother;
        $this->father = $father;
    }

    public function displayParents()
    {
        echo ("Mother=".$this->mother."; Father=".$this->father);
    }
}
?>

Animal.php:

<?php

abstract class Animal {
    public abstract function Vocalise();
}

?>

1 Comment

  1. ron todd says:

    A very minor comment: the class HomoSapien should perhaps be called HomoSapiens: “thinking man”, or “being-wise man”; in either case it wants to be a Latin present participle, with the much-loved -ens suffix. Or to put it another way, the “s” isn’t there to make it plural.

    Told you it was very minor.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s