How to Create a PHP Template Class - dummies

By Steve Suehring, Janet Valade

The heart of the template system is a PHP class that’s responsible for gathering together the various parts of a given page. The Page class includes a few methods and properties. You instantiate the Page class as part of building each page. Follow these steps for this exercise:

  1. Open your text editor and create a new empty file.

  2. Place the following PHP code in the file:

    class Page
        public $type = "default";
        public $title = "My Web Site";
        public $titleExtra = ";
    }   //end Page class
  3. Save the file as classPage.php in your document root.

Look how the first part of this code breaks down. The class Page is created and these three properties are declared:

  • Type: This corresponds to the type of page being displayed. By adding a type property, you can change the behavior of the various methods based on whether the type is default or another type. (This example has only a default type.)

  • Title: This appears in the browser’s menu bar.

  • Extra title: Use this for additional pages, so that the pages can have different titles.