Ch3 Notes
Ch3 Notes
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
1
Member Variable − These are the variables defined inside a class. This data will be invisible
to the outside of the class and can be accessed via member functions. These variables are
called attribute of the object once an object is created.
Member function − These are the function defined inside a class and are used to access object
data.
Inheritance − When a class is defined by inheriting existing function of a parent class then it
is called inheritance. Here child class will inherit all or few member functions and variables
of a parent class.
Parent class − A class that is inherited from by another class. This is also called a base class
or super class.
Child Class − A class that inherits from another class. This is also called a subclass or derived
class.
Polymorphism − This is an object oriented concept where same function can be used for
different purposes. For example function name will remain same but it take different number
of arguments and can do different task.
Overloading − a type of polymorphism in which some or all of operators have different
implementations depending on the types of their arguments. Similarly functions can also be
overloaded with different implementation.
Data Abstraction − Any representation of data in which the implementation details are hidden
(abstracted).
Encapsulation − refers to a concept where we encapsulate all the data and member functions
together to form an object.
Constructor − refers to a special type of function which will be called automatically whenever
there is an object formation from a class.
Destructor − refers to a special type of function which will be called automatically whenever
an object is deleted or goes out of scope.
2
function myfunc ($arg1, $arg2) {
[..]
}
[..]
}
?>
Here is the description of each line −
The special form class, followed by the name of the class that you want to define.
A set of braces enclosing any number of variable declarations and function definitions.
Variable declarations start with the special form var, which is followed by a conventional $
variable name; they may also have an initial assignment to a constant value.
Function definitions look much like standalone PHP functions but are local to the class and
will be used to set and access object data.
<?php
class Books {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
3
echo $this->title ." <br/>";
}
}
$physics = new Books();
$maths = new Books();
$chemistry = new Books();
$physics->setTitle( "Physics for High School" );
$chemistry->setTitle( "Advanced Chemistry" );
$maths->setTitle( "Algebra" );
$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();
4
Q. Write a PHP script to define a class Fruit with data members name and color. Accept and
display data for one object.
Ans:
<?php
class Fruit {
// Properties
public $name;
public $color;
// 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;
}
}
5
i. Public – the property ie. Data member and functions which are defined using public access
specifier can be accessed from outside of class.
ii. Private - the property ie. Data member and functions which are defined using private access
specifier can be accessed by the members of class only. No access is granted to outside of the
class.
iii. Protected - the property ie. Data member and functions which are defined using public access
specifier can be accessed from the class and its inherited i.e child class.
Example:
<?php
class Fruit {
public $name;
protected $color;
private $weight;
}
function myPublicFunction() {
return("I'm visible!");
}
6
Example of protected
class MyClass
{
protected $car = "skoda";
$driver = "SRK";
function myPublicFunction() {
return("I'm visible!");
}
7
3.2 Constructor and Destructor
function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
8
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
9
protected function intro()
{
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
10
echo “Inside class A”;
}
}
class B extends A
{
function showB()
{
echo “Inside class B”;
}
}
$obj=new B();
$obj->showA();
$obj->showB();
?>
11
function showC()
{
echo “Inside class C”;
}
}
$obj=new C();
$obj->showA();
$obj->showB();
$obj->showC();
?>
12
}
}
$obj1=new B();
$obj1->showA();
$obj1->showB();
$obj2=new C();
$obj2->showA();
$obj2->showC();
?>
13
$obj->add(5,2); //it will invoke function-2
?>
Overriding
- If a subclass contains a method with same name and signature as it is defined in its
superclass, it is called as overriding.
- Example
<?php
class A
{
function show()
{
echo “Inside class A”;
}
}
class B extends A
{
function show()
{
echo “Inside class B”;
}
}
$obj1=new A();
$obj1->show(); // it will call superclass i.e class A method
$obj2=new B();
$obj2->show(); //it will call subclass i.e class B method
?>
14
- An abstract method is a method that is declared, but not implemented in the code.
- An abstract class or method is defined with the “abstract” keyword:
- Syntax
abstract class MyAbstractClass {
abstract function myAbstractFunction() {
}
}
- 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>";
15
$volvo = new volvo("Volvo");
echo $volvo->intro();
echo "<br>";
?>
16
<?php
class Fruit {
final public function intro() {
// some code
}
}
17
{
echo "OOP is fun! ";
}
}
class Welcome
{
use message1;
}
$obj = new Welcome();
$obj->msg1();
?>
example of multiple traits
<?php
trait message1 {
public function msg1() {
echo "OOP is fun! ";
}
}
trait message2 {
public function msg2() {
echo "OOP reduces code duplication!";
}
}
class Welcome {
use message1;
}
class Welcome2 {
use message1, message2;
}
18
$obj->msg1();
echo "<br>";
19
</body>
</html>
<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>
20
Q What is introspection? Explain with suitable example.
Ans:
Introspection
- Is the ability of a program to inspect characteristics of object and class, such as name, parent
class, properties and methods.
- With introspection , we can write code which works on any class or object.
- 1) examining classes
$yes_no = class_exists(class_name);
- This method is used to check whether a class exists or not. It returns Boolean value.
$classes = get_declared_classes();
- This method returns an array contacting names of classes which are defined.
$methods = get_class_methods(classname);
- This method returns methods which are present in a class.
$properties = get_class_vars(classname);
- This method returns properties (data members) which are present in a class.
$superclass = get_parent_class(classname)
- This method returns parent class of given classname.
<html>
<body>
<?php
class A
{
public $x;
function initA($a)
{
$this->x = $a;
}
function showA()
{
echo "X:".$this->x;
}
}
class B extends A
21
{
public $y;
function initB($b)
{
$this->y = $b;
}
function showB()
{
echo "Y:".$this->y;
}
}
class C extends B
{
public $z;
function initC($c)
{
$this->z = $c;
}
function showC()
{
echo "Z:".$this->z;
}
}
if(class_exists('B'))
{
$obj1=new B();
echo "Class B exist";
}
else
echo "Class B does not exist";
22
$obj2=new C();
echo " Parent class is -".get_parent_class($obj2);
echo "<br>";
echo "Method of Class B are-";
$m = get_class_methods($obj1);
print_r($m);
echo "<br>";
echo "Properties of Class B are-";
$v = get_class_vars('A');
print_r($v);
echo "<br>";
?>
</body>
</html>
23