Thursday 29 April 2021

Php: Classes and objects

 

Classes are used to group functions and variables in a single unit. Once you define a class, you can instantiate any number of objects from that class.

 

Objects and class in Real World scenario

 

Object

An Object is a real world entity. For example, book, cat, box, bus, keyboard, person, screen, cell phone etc., all are objects.

 

So How can we differentiate objects

1.   Depend on their properties

2.   Depend on their behaviors

 

Differentiating two objects based on their properties

Let’s say, there are two persons A and B, we already discussed, each real time entity is an object. So these two persons are objects.

 

First let’s try to find what are the possible properties for the person object

 

Possible properties of a person

height, weight, colour, Education Qualification, country, address are all come under person properties.

 

Now by comparing these properties we can easily differentiate two persons.

Properties

Person A

Person B

Height

6ft

5.5ft

Weight

75Kg

60Kg

Color

white

Black

Country

India

America

As shown in the above table, both the persons has their own properties, So we can differentiate based on them.

 

Differentiating two objects based on their behaviour

consider the same two persons A and B. will try to figure out possible behaviours for the person object

 

Possible behaviours are

walk, run, swim, eat, sleep, see, hear etc., Now by comparing these behaviors we can easily differentiate two persons. 

Behavior

Person A

Person B

Walk

10km/hr

12km/hr

run

20km/hr

18km/hr

sleep

8 Hrs/day

6Hrs/day

 

Class

In normal terms I call class as a category.

 

All persons come under Human category

cat, lion, Elephant come under Animals Category

 

So a class or category is a group of objects with similar properties and behaviours.

 

Objects and class in Programming Terminology

 

What Is an Object?

An object is a software bundle of related state and behavior. The main advantage of java is you can compare objects in Java with real world objects. So it is very easy to work with objects in Java

 

What Is a Class?

A class is a blueprint or prototype from which objects are created.

 

 

How to define a class?

You can define a class using ‘class’ keyword.

 

Syntax

class ClassName{
    
}

 

As per the convention, class name starts with a capital letter and every first character in subsequent word also a capital letter.

 

Example 1

class Person{
    
}

 

Example 2

class EmployeeInfo{
    
}

 

What are properties and methods?

Variables defined in a class are called properties and functions defined in a class are called methods.

 

How to define properties in a class?

Using ‘var’ keyword, you can define properties in a class. For example, following snippet defines four variables $id, $first_name, $last_name and $age.

 

class Employee{

    var $id;
    var $first_name;
    var $last_name;
    var $age;
    ......
    ......

}

 

What is constructor?

Constructor is used to instantiate an object at the time of creation. You can define a construction using ‘function’ keyword and __construct function.

 

class Employee{
    var $id;
    var $first_name;
    var $last_name;
    var $age;

    function __construct($id, $first_name, $last_name, $age){
        $this->id = $id;
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->age = $age;
    }
    ......
    ......
}

$this is used to refer current instance or object properties.

 

Public and private methods

Within a class, you can define both public and private method. Public methods can be called by an object, whereas private methods are accessible within the class.

 

You can define a public method using ‘public’ keyword.

public function set_age($new_age){
    $this->age = $new_age;
}


You can define a private method using ‘private’ keyword.

private function full_name(){
    return $this->get_first_name() . ',' . $this->get_last_name();
}


How to define an object?

An object is created using new keyword followed by class name and constructor arguments.

 

Example

$emp1 = new Employee(1, 'Krishna', 'Gurram', 31);

 

How to call the methods of an object?

Syntax

$object_name -> method_name(arguments);

 

Example

$emp1->to_string();

 

Find the below working example.

 

 

class_demo_1.php

#!/usr/bin/php

<?php

class Employee{
    var $id;
    var $first_name;
    var $last_name;
    var $age;

    function __construct($id, $first_name, $last_name, $age){
        $this->id = $id;
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->age = $age;
    }

    // Gettter methods
    public function get_id(){
        return $this->id;
    }

    public function get_first_name(){
        return $this->first_name;
    }

    public function get_last_name(){
        return $this->last_name;
    }
    
    public function get_age(){
        return $this->age;
    }

    // Setter methods
    public function set_id($new_id){
        $this->id = $new_id;
    }

    public function set_first_name($new_first_name){
        $this->first_name = $new_first_name;
    }

    public function set_last_name($new_last_name){
        $this->last_name = $new_last_name;
    }

    public function set_age($new_age){
        $this->age = $new_age;
    }

    public function to_string(){
        return $this->get_id() . ', ' . $this->full_name() . ', ' . $this->get_age();
    }

    // private methods
    private function full_name(){
        return $this->get_first_name() . ',' . $this->get_last_name();
    }
}

$emp1 = new Employee(1, 'Krishna', 'Gurram', 31);
$emp2 = new Employee(2, 'Chamu', 'Gurram', 30);

echo"id, name, age\n";
echo $emp1->to_string();
echo "\n";
echo $emp2->to_string();

?>


Output

$./class_demo_1.php 

id, name, age
1, Krishna,Gurram, 31
2, Chamu,Gurram, 30






 

 

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment