Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

oop in php

/* We keep data memebers private and keep functions public. And access that data memebers through functions because we cannot access private data members directly. 
    We initialize data members through Constructor. */                                      


class Calculation{
     public $a, $b, $c;
     function sum(){
         $this->c = $this->a+$this->b;                                
         return $this->c;
     }
     function sub(){
         $this->c = $this->a-$this->b;
         return $this->c;
     }
 }


 $c1 = new Calculation();                    // create an object c1.
 $c2 = new Calculation();                    // create an object c2.

 $c1->a = 40;                                // this is not a good method to access and initialize data members. We keep data memebers private and keep function public. And access that data memebers through functions. We initialize data members through Constructor.   
 $c1->b = 30;

 echo "Sum of c1 = " . $c1->sum() . "<br>";
 echo "Sub of c1 = " . $c1->sub() . "<br>";

 $c2->a = 30;
 $c2->b = 20;

 echo "Sum of c2 = " . $c2->sum() . "<br>";
 echo "Sub of c2 = " . $c2->sub() . "
";
Source by www.greycampus.com #
 
PREVIOUS NEXT
Tagged: #oop #php
ADD COMMENT
Topic
Name
9+4 =