oop with php code example
Example 1: object oriented programming php
<?php
class Parent {
public function __construct() {
echo "Parent Created\n";
}
public function sayHello() {
echo "Hello, from Parent\n";
}
public function eitherHello() {
echo "Hello, from Parent and child if desired\n";
}
}
class Child extends Parent {
public function __construct() {
echo "Child Created\n";
}
public function sayHello() {
echo "Hello, from Child\n";
}
}
$p = new Parent();
$c = new Child();
$p->sayHello();
$c->sayHello();
$p->eitherHello();
$c->eitherHello();
?>
Example 2: oop php
<?php
class Mobile {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."
";
}
function setName($par){
$this->title = $par;
}
function getName(){
echo $this->title ."
";
}
}
$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();
?>