Tuesday, 15 November 2016

PHP OOP Hmelhriatna

PHP Object Oriented Bul leh bal:
Object Oriented Programming-a bet tlat term pawimawh te hi lo hmelhriat ve ila.

Class: Class hi OOP paradigm tawngkam lar tak a ni a. abstraction, modularity leh a dang tam tak-ah kan code min pui a ni.
Member function: Class chhunga function kan siam te.
Member Variable: Class chhunga variable kan siam te sawina a ni. Heng varibles te hi Object attribute tiin an sawi bawk.
Object: Object hi i class siam entirna (aiawhtu=instance) a ni. Class i siam a, object tam tak i nei thei. Object hi instance tia sawi a ni bawk.
Inheritance: Inheritance hi OOP hmanraw tangkai ber te zinga mi a ni. Inheritance hian i class siam tawh (base class/parent class) hmanga class tam tak siam theihna a ni.
Parent class: Class  danga inghahna class base class or super class tia sawi a ni bawk.
Child Class: Class dang atanga class peng. subclass or derived class an ti bawk.
Polymorphism: Poly (tam tak tihna) and morph (forms tihna). behaviors hranga operation thuhmun.
Overloading: Overloading chu child class-a parental behaviour thlak tihna.
Data Abstraction: Data entirna a ni a, ahmanna tak tak chu thuhruk (abstract) vek a ni.
Encapsulation: OOP that bikna em em chu data thuhruk (encapsulation) hi a ni. Class pawn atanga data direct access theih loh turin hide vek a ni.
Constructor: Class Constructor nei chuan he method hi object kan siam apiangin a ko chhuak thei a. Object kan siam hmaa bul tanna siamna a ni ber.
Destructors: Scope pawn lama object a kal emaw delete a nih apianga Function automatic-a kan kohchhuah theih.
 
Structuring Classes
Class siam hi thil mawlmang ve tak leh awlsam tak Class tih thumal hmanga kan chinfel theih mai a ni.

<?php
    class myFirstClass {
        #code
    }
?>
  Hei hi i class i siam dan tur leh a chhunga i code dah dan tur a ni.

     <?php
    class kaClassHmasaber {

        function kaFunction(){
            echo "Ka function";
        }
 }
?>
Member Variable:
<?php
    class kaClassHmasaber {
        $var1 = "hello";
        $var2;
    }
?>
Objects siam dan-PHP

I class chu i siam tawh chuan Object chu i duh zat zat i nei thei ta.

<?php
$objc1 = new kaClassHmasaber();
$objc2 = new kaClassHmasaber();
?>
Hei hi Class atanga Object kan siam dan a ni.

Class Member function hmanchhuah dan.

<?php
  $obj = new kaClassHmasaber(); // object of the class

  $obj->kaFunction(); // member function call
?>

Inheritance
PHP class chu parent class atangin extends keyword hmangin inhlanchhawn theih a ni.
<?php
    class kaClassHmasaber {

        function kaFunction(){
            echo "function 1";
        }

    }
    class childClass extends kaClassHmasaber  {
        #code
    }
?>
child class (or subclass or derived class) characteristics hetiang hian a nei a:

1. Member variable declaration.
2. Same member functions as the parent and all work same as parent work.
Here childClass is the the Child Class of Parent Class myFirstClass.

Polymorphism

Polymorphism hi behaviour hrang operation in ang: entirnan ramsa(animal) tawng dan (talk beviour)  chu a in ang a mah se a ri (sound) chu ramsa zawng zawngah a danglam vek a ni.
<?php class animalClass {
      protected $name;
      public function __construct($animalName)
      {
         $this->name = $animalName;
      }

      public function getName()
      {
         return $this->name;
      }

      public function talk()
      {
         return $this->getName().' is talking <br />';
      }
   }

   class dogClass extends animalClass
   {
      public function getName()
      {
         return 'Dog: '.parent::getName();
      }
   }

$a = new dogClass("My dog");
echo $a->talk();
$b = new animalClass("some Cat");
echo $b->talk();
?>
Ramsa chi hrang hrangin aw chi hrang vek an nei. Mahse chu chu an tawng ve dan a ni.

Overloading

Class-in hming in ang Function pahnih or a aia tam a neih a, number hrang/data types of paramerters hrang a accept-in .
PHP-ah chuan run-time-a Object members heng __set,__get, __call hmanga kan add hian Overloading an ti.

<?php
class myClass {
    public function __call($method, $args) {

        if ($method === 'myFunction') {
            echo 'Sum is calculated to ' . $this->getSum($args);
        } else {
            echo "Called method $method";
        }
    }

    private function getSum($args) {
        $sum = 0;
        foreach ($args as $arg) {
            $sum += $arg;
        }
        return $sum;
    }
}

$obj = new myClass;
$obj->myFunction(10,25,30,12);
$obj->myFunction(21,20,1);
?>
Encapsulation
Class pawn atanga data access theihna hide tu. Encapsulation hi chi thum a awm thei a:

1. Public Members: Hei hi methods and properties of a class default state a ni, class pawn atang pawha luh theih a ni a, class chhung atang pawhin a khawih theih a, a class kan hmanna class dangah pawh acccess theih a la zel bawk.

Class chu a pawn lama hman theih loh tura i dah duh chuan a member te chu private or protected-in a dah thei ang.

2. Private Members: Class member chu private-a i dah tawh chan class kan declare (siamchhuahna) ngei ngeiah pawh khawih turin tihdan bik hman a ngai tawh a ni. Class Private-a dah dan chu a member hmaah Private keyword hman mai tur a ni.
<?php
    class myFirstClass {
        function myFunction(){
            echo "public one";
        }
        private function myPrivateFunction(){
            echo "I'm  not visible outside!";
        }
    }
?>

Class danga he class hi extend a nih pawhin access myPrivateFunction hi access theih a ni lo, a private property vang hian.

3. Protected Members: Class chhunga property or method private-a kan dah tawh chuan, a class atangin access theih a ni a. Class property-a protected-a kan dah dawn chuan a member hmaah dah tur.

<?php
    class myFirstClass {
        function myFunction(){
            echo "public one";
        }
        protected function myprotectedFunction(){
            echo "I'm  visible in child class!";
        }
    }
?>

Constructor Functions:
Constructor function chu class atanga object kan siam function kha a ni. Object kan siamnaa thil tam tak bul tanna tura siam a ni.*/
<?php
    class myFirstClass {
        __construct($par1, $par2){
            $this->price = $par1;
            $this->title = $par2;
        }
        function myFunction(){
            echo $this->price." ".$this->title;
        }
    }
    $obj = new myFirstClass('Hello',7);
    $obj->myFunction();
?>
PHP hian function special tak __construct() hi constructor siam nan a nei sa. Constructor function-ah hian argument i duh zat zat i dah thei a ni.

Destructor:
Constructor ang thovin  __destruct() hmangin kan siam thei a. Destructor chhungah chuan resources zawng zawng i release thei a ni.