PhpTips.im - Everything about php - Download CMS, find tips and tricks, code snippetz, classes and more and more...
Bookmark this page !
   

Articles

    Home » Articles » PHP Classes » Introduction to php Classes -Hello World Example
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.




Code:

<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>
 

 
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.




Code:

 
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 '
'
.$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.




Code:

<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>
 
('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




Code:
$helloworld->



Refers to the variable we set our class into :



Code:
$helloworld = new Helloworld;

This next part
Code:
shout('Hello world!');

Refers to the function inside our class :
Code:
public function shout($message) {
 
echo $message;
 
$this->our_echo = $message;
 
}






Add this article in :

 

There is not yet any comment !



15 Rating: 6.7/10 (3 votes cast)

 

 Members
Log In !
Email Address :

Password :


Change my passowrd !
Sign Up !
We have : 3 members.
Latest member : albans
 
 Last Posts
 
 Advertising
 
©PhpTips.im 2010
Powered by PikaCMS.
Quick Links :
Home | Browse all articles | Downloads | Terms of Use
RSS RSS 2.0 : Articles | Downloads ATOM : Articles | Downloads