Encapsulation is protection mechanism for your class and data structure. It makes your life much easier. With Encapsulation you have a control to access and set class parameters and methods. You have a control to say which part is visible to outsiders and how one can set your objects parameters.

Encapsulation means simply wrapping up – ‘member functions’ & ‘member variables’ into class. Then we can use class’s member variables & functions using its objects.

In a class, variables are called properties and functions are called methods!

The $this Keyword

this keyword refers to the current object, and is only available inside methods.

Let’s write a PHP code, for simple class construction

File – code1.php

<?php
class animal{
    function cat(){
     echo “animal is cat”;
    }
}
$puss = new animal();
$puss->cat();
?>
output

Let’s write a PHP code, for constructing a class with member functions & member variables.

File – code2.php

<?php
class asia{
    // member variables = properties
    public $con;
    // member functions = methods
    function country(){
        $con=’india’;
        echo “country is: $con”;
    }
}
 
$in = new asia();
$in->country();
?>
output

Let’s write a PHP code, for passing arguments into class’s functions.

File – code3.php

<?php
class asia{
    // properties ‘con’,’PM’
    public $con;
    public $PM;
    // parameters ‘a’,’b’
     function country($a,$b){
        $con=$a;
        $PM=$b;
        echo “country is: $con”;
        echo “<br>Prime minister is: $PM”;
    }
}
 
$in = new asia();
// arguments ‘INDIA’,’narendra modi’
$in->country(‘INDIA’,’narendra modi’);
?>
output

Let’s write a PHP code, accessing methods & properties of a class using objects.

File – code4.php

<?php
class bird{
    public $sound=”def”;
    public $color=”def2″;
    function crow(){
       $sound=”cwaw”;
      $color=”black”;
      
        echo “crow sound: $sound”;
        echo “<br>crow color: $color”;
    }
    function parrot($snd,$col){
       $sound=$snd;
       $color=$col;
        
        echo “<br>parrot sound: $sound”;
        echo “<br>parrot color: $color”;
    }
}
 
$c1 = new bird();
$p1 = new bird();
 
$c1->crow();
$p1->parrot(‘cwak’,’green’);
 
?>
output

Let’s write a PHP code, to shows ‘this’ keyword uses.

File – code5.php

<?php
class animal{
    function dog($name){
            $this->name=$name;
            echo “dog name is: $name”;
    }
}
$d1 = new animal();
$d1->dog(‘rocky’);
?>
output

  • Constructor

A constructor allows you to initialize an object’s properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class

PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

No-argument constructor

File – code6.php

<?php
class animal{
    public function animal(){
        echo “default animal constrcutor!”;
    }
}
$cat = new animal();
?>
output

Parameterized constructor

The user-defined constructor that contains an argument is called a parameterized constructor. The parameter values of this builder are supplied when the object is being created and this constructor can also call the other methods of the class.

File – code7.php

<?php
class animal{
    public function animal($name,$sound,$color){
        $this->name=$name;
        $this->sound=$sound;
        $this->color=$color;
        echo “animal name: $name,sound: $sound,color: $color”;
    }
}
$cat = new animal(‘pussy’,’mweow’,’grey’);
?>
output