Defining a Class in PHP

Defining a Class in PHP

Defining a Class in PHP

A class is a container that holds code used to perform a specific task. A class is more complex than a PHP function. In fact, classes usually contain functions, except they are called methods when you are talking about them and functions when you are creating them. Variables existing within classes are called properties.

Classes allow for reusable code that can be a part of many different projects. Using classes also results in cleaner looking code. That’s because the main program files simply pass variables to the classes. The bulk of the code that performs the task is hidden in the class file.

Creating a Class

It’s best to create a separate file to hold your class. In fact, you can create a library of classes and keep them all in one file if you want. That way you can simply include that file whenever you are working on a project requiring access to that class.

Let’s create a simple class designed to output an HTML page containing variables passed to it from an outside program. We’ll call it MyTemplate.


class MyTemplate {


There. We’ve created a new class! Wow, impressive, right? Well, not really. It doesn’t do anything yet. Let’s fix that.


class MyTemplate {
var $PageTitle;
var $PageContent;

It still doesn’t do anything, but we’re getting closer. We’ve set the class up to receive two variables. These variables will be used later to create the page output. Let’s get to that part since it’s where all the action is.

We are going to create 3 methods (functions) and add them to our existing class template.


class MyTemplate {
var $PageTitle;
var $PageContent;

function ShowPage( ) {
/* this will output the complete HTML page when called from index.php */

echo “<HTML>n<HEAD>n”;
$this->ShowTitle( );
echo “n</HEAD>n<BODY>n”;
echo $this->PageContent;
echo “n</BODY>n</HTML>n”;

function ShowTitle( ) {
// displays browser title field
//called from ShowPage()

echo “<TITLE>” . $this->PageTitle . “</TITLE>n”;


function SetContent( $PageContent ) {
// called by index.php
// sets the body text
$this->PageContent = $PageContent;

That’s everything we need for the Class. Now let’s create a PHP page that uses the class.


include “myclass.php”;

$MyPage = new MyTemplate; // new instance of class mypage

$PageContent = “<P>This is a sample page I created using my new Class.</P>”;

$MyPage->PageTitle = “Defining a Class in PHP”;
$MyPage->SetContent( $PageContent );

$MyPage->ShowPage( );


Try it Yourself

Copy all of the code above between <— START CLASS CODE — > and <—END CLASS CODE —> and save it to a file named myclass.php (or whatever you want to name it).

Then copy all of the code above between <— START INDEX CODE — > and <—END INDEX CODE —> and save it to a file named index.php.

If you renamed the myclass.php file, be sure to change the include statement in index.php to match the new name.

Now run index.php for yourself and watch the results.

For Practice

Create a form that allows you to modify the page title and content before calling the class. Then try adding a new method to the class file that uses the existing page title variable and also outputs it as a page heading in <h1> tags.

Previous Post
Visual Studio 2008

Chapter 1 from Professional Visual Studio 2008

Next Post
Honda's Robotic Legs
Artificial Intelligence

Robotics That Help You Walk


    • celiaonegirl
    • October 17, 2009

    Hi to all of you!
    I’m new here and i hope i can learn many things here.

    • Joinston
    • January 10, 2013

    One of the hardest parts of PHP for me to learn was classes. I find myself trying to avoid them and always use functions but this made it simple and now I understand it much better, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *