PHP Object oriented programming with classes and functions.

Author:Kostas Sp Published On:September 19, 2014 Under:Php

 
 

In this post we will try to found out how we can organize our code in php in objects
and understand the fundamentals of object oriented programming.Are you ready?


 

<?php
 
class Example { 
  // scope: where you can access the variable outside of the class    
  // php4: var
  // php5: private,public or protected    
  // php6: var interchangable with public    
  var $item ='item_name_1';
  public $name;
 
  function Sample() {  
    $this ->Test();
  }
 
  function Test() {
   echo $this->item; // print variables
  $regular ="THIS IS A REGULAR VARIABLE2";
  echo $regular;
 
    }  
}
 
// instanciate the object here
 $example =  new Example();
 $example->Sample();
 
?>

$this applies to the all items in class Example
and means:Inside of Example class call the the Test function
 
The comments inside the class explain certain things but generally the idea is that
the class example contains 2 functions Sample and Test.
 
The function Sample calls the function Test so the output comes from the function Test.

   
Tags:
 

Leave a Reply


3 + = four

.
 
Categories
TEST

Anything in here will be replaced on browsers that support the canvas element

Tags