Friday, April 15, 2011

PHP Class

Classes in Php are constructed similar to the class's in java-script using the this keyword. When calling a method in Php, the syntax is name_of_class->name_of_method. Before calling the method you must initialize it using a variable and the new keyword.

Example:

$myclass = new name_of_class;
$myclass->name_of_method();

When using class's in Php you construct the class similar to java-script. A construct method is used to store the variables used in the class, and is accessed using the name_of_class->$variable. When sending information to the screen with variables the syntax is as follows: echo "The hen laid an,".$name_of_class=$variable. To use a parameter passed into the function, the syntax is similar.

Example:

echo $this->name_of_class.$parameter

To configure the class use the class keyword. The construct is defined as public function __construct().

Example:

//The example was taken from something I needed. It takes a directory path as a parameter, and then //displays the contents of that directory. Since Ignore the string1 variable and the line that says, echo //"Show_New_Apps"."[BRK]".$string1."[BRK]";.  I will be using that code to output the screen //contents to a specific area of the screen, using Ajax, for the project I am working on. The contents //is sent back, and split off into an array. It is parsed, evaluated, and placed into the correct tag.

class file_operations
  {
 
    // constructor
    public function __construct() {
        $this->test = 120;  //Define variable
    }
   
    public function list_rentalapp_docs($directory) {
        $count=0;
        $clr="#CCFFFF";
        $dir = $directory;
       
        // Open a known directory, and proceed to read its contents
        //if (is_dir($dir)) {
        //echo "yes";
           if ($dh = opendir($dir)) {
               while (($file = readdir($dh)) !== false) {
                   $count=$count+1;
                  
                   if ($count==2){
                           $clr="#66CCFF";
                           $count=0;
                   }
                   else
                   {
                           $clr="#CCFFFF";
                   }
                  
                   $string1 .= "
";
                   $string1 .= "
";
                   $string1 .= "$file";
                   $string1 .= " ";
                   $string1 .= "Delete";    
                   $string1 .= "
";
                          



                 echo ""."$file"." "."Delete"."";
                  //echo "Show_New_Apps"."[BRK]".$string1."[BRK]";

               }
               closedir($dh);
           }
      }
  }


Use the class as part of your script or call it in using the require function. An example of this function can be found at fpdf.org.

For more information visit the site for the Php manual. This was a basic tutorial on Php classes. It is a starting point. There is a lot more complexity involved, but this is the foundation. For most programming projects, this is all you need. Always keep it simple. I hope you enjoyed this tutorial and found it informative.

Additonal Resources:

Php Manual 

Recommended PC Products

  • Speed up your computer. 
  • Permanently delete files.
  • Fix registry problems
  • Uninstall software without registry traces
  • Clean up the tracks of your Internet browsing

Get PC Optimizer Pro

Get started with your web design dreams!

Get the Web Design Business Start Up Kit. A 200 page Ebook with 67 ready made contracts. 

If you want your own web design business, but don't know where to start, this book will get you started.  

See the first 3 chapters for free.








Programming for the Web