Benjamin Lu

everyday is cool

Trying a Class in the PHP 5 Era

Written retrospectively for the 2004 archive.

PHP 5 arrived in July 2004 with a revised object model. To see one reason to use a class, try keeping the parts of a page title together in a small object. The example can run on a modern local PHP installation:

<?php
class PageTitle {
    private $site = 'My Web Notes';

    public function format($page) {
        return $page . ' | ' . $this->site;
    }
}

$title = new PageTitle();
echo $title->format('About');

Run the file and check for About | My Web Notes. Try a second page name with the same object. Both titles follow the same format because the separator and site name are kept in one place. If you use a title like this in real HTML, escape it at the point where you render it.

PHP project history