class rect
{
   public $length=0;
   public $width=0;
   public $area=0;
   public function getData()
   {
      $this->length=10;
      $this->width=20;
   }
   public function area()
   {
      $this->area = $this->length * $this->width;
   }
   public function showArea()
   {
      echo "Area of $this->length and $this->width is $this->area";
   }
}
    $rect = new rect();
    $rect->getData();
    $rect->area();
    $rect->showArea();
?>
What is a class?
 A class represents an object, with associated methods and variables.
 Class is nothing without creating an object.To create a class we use keyword 'class'.
 Class Definition is  Similar to defining a function......The definition does not do anything by
 itself. It is a blueprint, or description, of an object. To do something, We need to use the class.
 An example class definition for a rect:-
 The rect object has three attributes, length, width and area and can perform the action of area.
 Use of $this in a class:-
  The $this is a built-in variable (built into all objects) which points to the current 
  object. Or in other words, $this is a special self-referencing variable. we use $this 
  to access properties and to call other methods of the current class.
Instantiate/create your object:-
  Classes are the blueprints/templates of php objects. Classes don't actually become 
  objects until you do something called: instantiation.
The 'new' keyword:-
  To create an object out of a class, you need to use the 'new' keyword. 
Steps:-
 class rect {           // Define the name of the class.
 public $length; public $width; public $area;  // Define an object of attribute (variables) the length and width of a rectangle.
public function getData(), public function area() and public function showArea() are object actions (functions).
$rect = new rect();       //Create a new instance of the class Or new object of the class.
$rect->getData(); $rect->area(); and $rect->showArea(); 
   //Use the rect object and methods getData(), area() and showArea().
                       
                    
0 Comment(s)