PHP Knowledge Base

OOP Examples from greycampus.com

Example #1
<?php 
class Books{
	public function name(){
	echo "Drupal book<br>";
	}
	public function price(){
	echo "900 Rs/-";
	}
}

$obj = new Books();
$obj->name();
$obj->price();
?>

demo

Example #2
<?php 
   class Mobile {
      /* Member variables */
      var $price;
      var $title;
      /* Member functions */
      function setPrice($par){
         $this->price = $par;
      }
      function getPrice(){
         echo $this->price ."<br>";
      }
      function setName($par){
         $this->title = $par;
      }
      function getName(){
         echo $this->title ."<br>";
      }
   }
$Samsung = new Mobile();
$Xiaomi = new Mobile();
$Iphone = new Mobile();
$Samsung->setName( "SamsungS8" );
$Iphone->setName( "Iphone7s" );
$Xiaomi->setName( "MI4" );
$Samsung->setPrice( 90000 );
$Iphone->setPrice( 65000 );
$Xiaomi->setPrice( 15000 );
// Now you call another member functions to get the values set by in above example
$Samsung->getName();
$Iphone->getName();
$Xiaomi->getName();
$Samsung->getPrice();
$Iphone->getPrice();
$Xiaomi->getPrice();
?>

demo

Example #3

PHP program to build a class and store the data of the mobile phones with screen size,ram,company and processor along with Constructor and function to estimate cost of phone based on conditions

demo

NULL

demo