Right lets get straight to it as this is only a quick tip, do note though that we’re only scratching the very surface of PHP classes.

We’ll be creating two files, our main page index.php and our class file class.helloworld.php. The only thing we’ll need on our index.php is the code below and the calling of the class which we’ll do later on. All it’s doing is including our class file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
     <title>Untitled Document</title>
 
</head>
 
<body>
 
	<?php 
 
		include('class.helloworld.php');
 
	?>
 
</body>
 
</html>

The class

Next we’ll write this in our class file class.helloworld.php. I’ve added some comments to the code for you to get a grasp of it.

<?php 
 
	class Helloworld { // define the class
 
 
		var $our_echo; // set a class variable to store our echo
 
 
 
		public function shout($message) { // define a class function and make it public
 
			echo $message; // display the message in the functions argument on the index.php page
 
			$this->our_echo = $message; // set our var on line 6 to also contain the message
 
		} // end function shout
 
 
 
		public function shout_echo() { // define a class function and make it public
 
			echo '<br />'.$this->our_echo; // create a HTHML line break and display our variable, $this refers to the Helloworld class
 
		} // end function shout_echo
 
 
	} // end Helloworld class
 
	$helloworld = new Helloworld; // set the class Helloworld into a variable
 
?>

Calling the class

Now we need to call our class in our index.php file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
     <title>Untitled Document</title>
 
</head>
 
<body>
 
	<?php 
 
		include('class.helloworld.php');
 
		$helloworld->shout('Hello world!'); // call the shout function from the class with the argument 'Hello world!'
 
		$helloworld->shout_echo(); // call the shout_echo function which will repeat the message
 
	?>
 
</body>
 
</html>

This should then output:
Hello world!
Hello world!

Calling the class in a little more detail

This

$helloworld->

Refers to the variable we set our class into :

$helloworld = new Helloworld;

This next part

shout('Hello world!');

Refers to the function inside our class :

public function shout($message) { 
 
     echo $message; 
 
     $this->our_echo = $message; 
 
}

Author: Benjamin Reid

My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.

RSS Icon

Subscribe to the RSS feed some design goodness, get in on the upcoming software give-aways!

Digg Icon Share Quick Tip #5 - A ‘Hello world’ introduction to PHP classes to Twitter Share Quick Tip #5 - A ‘Hello world’ introduction to PHP classes to Delicious Share Quick Tip #5 - A ‘Hello world’ introduction to PHP classes to Facebook

Related post(s)

Comments left by other wizards

10 Responses | Make a comment

Leave your words of wisdom

Leave a Reply

This is a Gravatar

Your name