Published on : 01.05.2010 Category : PHP Classes Viewed : 53 times.
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.
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.
Code:
class Helloworld {// define the class
var$our_echo;// set a class variable to store our echo publicfunction 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 publicfunction shout_echo(){// define a class function and make it public echo' '.$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.