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 » Class Structures
Published on : 01.05.2010 Category : PHP Classes Viewed : 36 times.

For lack of a better example right now, i'm going to show you how to create a simple page object. The page object encompasses a few of the basic page necessities in building an HTML page. Now, there is a lot more you could do with this class, but i'm going to keep it simple for the time being. You guys can expand on it if want later. Okay, lets start by looking at what the basic elements of an HTML page are. First, you have the open and closing tags, and . Those will need to be included in the output. Also, you have the page Title, Keywords for the Metatag field, and naturally the main content, or body. Now that we know what we need, lets start building our class. 

First thing we need is the class itself: 


Code:


class Page {

 }
?>


This is a basic class. No variables, no functions, nothing - Completely empty.
All class structures look the same here with the exception of the 'Page' name. Every class/object is assigned a name for reference. You'll need to know this to create new copies of the object, so pick something straightforward and sensible. 

Next, we need to put in our variables. We need a title for the page, keywords, and a content body, like so:

Code:



class Page {
   var 
$Title;
   var 
$Keywords;
   var 
$Content;
 }
?>


Now, we could actually start using the class here. We have an object. But it isn't done - We still want to add some functions, to make it easier to work with. 

What functions do we need? Well, we need something to build the output HTML. Lets call that 'Display'. Lets create simple functions for displaying the Title and Keywords, also. Lets make a function for setting the content as well. 

The final class code is:



Code:
class Page {
   var 
$Title;
   var 
$Keywords;
   var 
$Content;

   function 
Display( ) {
     echo 
"nn";
     
$this->DisplayTitle( );
     
$this->DisplayKeywords( );
     echo 
"nnn";
     echo 
$this->Content;
     echo 
"nnn";
   }

   function 
DisplayTitle( ) {
     echo 
"" $this->Title "n";
   }

   function 
DisplayKeywords( ) {
     echo 
'. $this->Keywords '">';
   }

  function 
SetContent$Data ) {
     
$this->Content $Data;
   }
 }
?>


Now, at first glance this looks pretty simple. And you know what? It is. Its just basic PHP code wrapped into a class. Let me point a few things out before we go any farther though.

VAR -
 All variables declared in the class must be placed at the top of the class structure, and preceeded with the VAR statement. 
$THIS -
 $this is a variable that incidates the current object. That way, the object knows how to find itself while running functions contained within it. 
 For instance, $this->Keywords gets the data from the $Keywords variable in the object. You'll also notice that when using variables contained 
 within a class, you can't use the $ to reference them - But you have to use it to reference the object itself. 

Lets save this class file out before we continue. If your following along, save it out as page.class for now. You'll need it for the example coming up.  



Using the Class



Now that we have a class created, lets try using it in a normal script. 



Code:
include "page.class";

 
$
Sample = new Page;

$Content "

This page was generated by the Page Class example.

"
;

 
$
Sample->Title "Using Classes in PHP";
 
$
Sample->Keywords "PHP, Classes";
 
$
Sample->SetContent$Content );

 
$
Sample->Display( );

?>


Doesn't get much simpler than that, does it? We use the include statement to bring in the class from its external file. We use the 'new' statement to create a new copy of the object so we can work with it. The new copy is stored into a variable called $Sample. Then we just set some variables - The Title, Keywords and Content - And use the Display function to output it. Easy or what?



Add this article in :

 

There is not yet any comment !



13 Rating: 5.3/10 (4 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