29
Jul
Quick Tip #5 - A ‘Hello world’ introduction to PHP classes
Posted by Benjamin Reid in Quick Tips
Posted by Benjamin Reid in Quick Tips
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>
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 ?>
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!
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; }
My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.
This entry was posted on Wednesday, July 29th, 2009 at 12:54 pm and is filed under Quick Tips & tagged with Class, PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Quick Tip #5 – A 'Hello world' introduction to PHP classes | Nouveller | Webmaster Tools wrote on July 29, 2009
[...] Original post: Quick Tip #5 – A 'Hello world' introduction to PHP classes | Nouveller [...]
Whessasse wrote on August 1, 2011
Cock Enlargement Pills Surgury http://stonewalljacksoncarnival.org/ - Online Casino Game The skyrocketing popularity of online casino, since its arrival, is no doubt something that cannot be overlooked at any cost. Casino Money
Dusseldorf wrote on September 16, 2011
Escort Dusseldorf…
[...]php class tutorial | Quick Tip #5 - A ‘Hello world’ introduction to PHP classes at Nouveller | Nouveller[...]…
kidney disease in cats wrote on September 16, 2011
kidney disease in cats…
[...]php class tutorial | Quick Tip #5 - A ‘Hello world’ introduction to PHP classes at Nouveller | Nouveller[...]…
remove google redirect virus 2011 wrote on September 17, 2011
remove google redirect virus…
[...]php class tutorial | Quick Tip #5 - A ‘Hello world’ introduction to PHP classes at Nouveller | Nouveller[...]…
suc khoe wrote on September 17, 2011
Bai viet kha ly thu
bnc connectors wrote on September 17, 2011
bnc connectors…
[...]php class tutorial | Quick Tip #5 - A ‘Hello world’ introduction to PHP classes at Nouveller | Nouveller[...]…
moto kurye,istanbul kurye,acil kurye wrote on October 13, 2011
We’re a gaggle of volunteers and starting a brand new scheme in our community. Your site offered us with useful information to paintings on. You’ve performed a formidable task and our entire community shall be grateful to you.
SPFMoisturizers wrote on November 11, 2011
SPFMoisturizers…
[...]php class tutorial | Quick Tip #5 - A ‘Hello world’ introduction to PHP classes at Nouveller | Nouveller[...]…
iso 9001 ,norma iso 9001,normas iso 9001 ,iso 9001,iso9001,calidad iso ,sistemas iso ,gestion iso ,manual iso ,auditoria iso ,certificacion iso ,certificaciones iso ,implementar iso ,implementacion iso wrote on November 17, 2011
iso 9001 ,norma iso 9001,normas iso 9001 ,iso 9001,iso9001,calidad iso ,sistemas iso ,gestion iso ,manual iso ,auditoria iso ,certificacion iso ,certificaciones iso ,implementar iso ,implementacion iso …
[...]php class tutorial | Quick Tip #5 - A ‘Hello world’ introduction to PHP classes at Nouveller | Nouveller[...]…