Chapter - 3 Apply Object Oriented Concepts in PHP Marks-16: Notes by - G. R. Jagtap 1
Chapter - 3 Apply Object Oriented Concepts in PHP Marks-16: Notes by - G. R. Jagtap 1
Chapter - 3 Apply Object Oriented Concepts in PHP Marks-16: Notes by - G. R. Jagtap 1
Content outline:
3.1 Creating Classes and Objects
3.2 Constructor and Destructor
3.3 Inheritance, Overloading and Overriding, Cloning Object
3.4 Introspection, Serialization
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
$physics->setPrice( 10 );
$chemistry->setPrice( 15 );
$maths->setPrice( 7 );
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
?>
The variable $this is a special variable and it refers to the same object ie. itself.
Creating Objects in PHP
Once class is defined, then create as many objects as required of that class type. Following is an
example of how to create object using new operator.
$physics = new Books();
Calling Member Functions
After creating your objects, you will be able to call member functions related to that object. One
member function will be able to process member variable of related object only.
Following example shows how to set title and prices for the three books by calling member functions.
$physics->setTitle( "Physics for High School" );
$physics->setPrice( 10 );
$physics->getTitle();
$physics->getPrice();
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}
}
function myPublicFunction() {
return("I'm visible!");
}
function myPublicFunction() {
return("I'm visible!");
}
function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$obj=new C();
$obj->showA();
$obj->showB();
$obj->showC();
?>
- Example
<?php
// Parent class
abstract class Car {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function intro() : string;
}
// Child classes
class Audi extends Car {
public function intro() : string {
return "Choose German quality! I'm an $this->name!";
}
}
class Volvo extends Car {
public function intro() : string {
return "Proud to be Swedish! I'm a $this->name!";
}
}
// Create objects from the child classes
$audi = new audi("Audi");
echo $audi->intro();
echo "<br>";
trait message2 {
public function msg2() {
echo "OOP reduces code duplication!";
}
}
class Welcome {
use message1;
}
class Welcome2 {
use message1, message2;
}
<html>
<body>
<?php
$a = array('Manish',42,'Manager',50000);
// Serialize above data.
$ser = serialize($a);
// printing the serialized data
echo "Output of Serialize is - "."<br>".$ser;
echo "<br>";
// unserializing the data in $string
$unser = unserialize($ser);
// printing the unserialized data
echo "Output of Unserialize is - "."<br>";
print_r($unser);
?>
</body>
</html>
<html>
<body>
<?php
class A
{
public $x;
function initA($a)
{
$this->x = $a;
}
function showA()
{
echo "X:".$this->x;
}
}
class B extends A
if(class_exists('B'))
{
$obj1=new B();
echo "Class B exist";
}
else
echo "Class B does not exist";