All pastes #934949 Raw Edit

Someone

public text v1 · immutable
·published 2008-03-09 06:08 UTC
rendered paste body
<?php
class Page {
   var $Title;
   var $Keywords;
   var $Content;

   function Display( ) {
     echo "<HTML>\n<HEAD>\n";
     $this->DisplayTitle( );
     $this->DisplayKeywords( );
     echo "\n</HEAD>\n<BODY>\n";
     echo $this->Content;
     echo "\n</BODY>\n</HTML>\n";
   }

   function DisplayTitle( ) {
     echo "<TITLE>" . $this->Title . "</TITLE>\n";
   }

   function DisplayKeywords( ) {
     echo '<META NAME="keywords" CONTENT="' . $this->Keywords . '">';
   }

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

 }

?>


Lets analyze the above code.

VAR - All variables declared in the class must be placed at the top of the class structure, and preceded with the VAR statement.

$THIS - $this is a variable that indicates the current object. That way, the object knows how to find itself while running functions contained within it.

Lets save this class file as page.class. You'll need it for the example coming up.

Now that we have a class created, lets implement it.

<?php

 include "page.class";
 $Sample = new Page;
 $Content = "<P>This page was generated by the Page Class example.</P>";
 $Sample->Title = "Using Classes in PHP";
 $Sample->Keywords = "PHP, Classes";
 $Sample->SetContent( $Content );
 $Sample->Display( );

?>