magics methods in php

Hi Friends,

i am from outsource2global, which is best outsourcing company to meet client’s requirement on very minimal budget.

Magics methods in php –   It is one of the best methods that is developed in php . This methods start with double underscore (__) . There are following the list of magics methods .

  1. __construct

ans –        As we know that , It is called ,whenever need to create or initializing the object of class .

example –

— old constructor style —

 class abc {

 public $name;

function abc($name){

$this->name = $name;

}

$obj = new abc();

whenever we create of this class abc , the first function constructor , name of class going to execute first.then after that other function will be execut.

But , In new version of php , this constructor  name of class is deprecated , It means  we only need to define magis methods that is __constructor();

 

Example –

defined constructor in new veriosn php –

class abc

{

                              $public $name;

                                         __construct($name)

                                                                                                           {

                                                   $this->name = $name;

                                                                                                             }

}

                                    $obj = new abc(‘rohit’);

 

 

 

2  . __destruct

3 .__set

 

ans –  There are two methods is in php one getter and second setter are very important to chage and access value of private property – i am just giving example

 

class Animals {

  public function __construct($id) {
    $this->getlions($id);
  }

  public function getlions($id) {
    //code goes here
  }

  public function __get($name) {
    if($name == 'name') {
      return $this->username;
    }
  }

  public function __set($name, $value) {
    if($name == 'name') {
      $this->username = $value;
    }
  }
}

 

4. __get

class Animals {

  public function __construct($id) {
    $this->lions($id);
  }

  public function lions($id) {
    //  code goes here
  }

public function __get($name) {
    if($name == 'name') {
      return $this->username;
    }
}
$bx = new Animals (3);
echo $bx->name . " is " . $bx->age . " years old\n";

 

note –  here $bx->name is unaccessible property, so from the help of php magis methods __get , we can access the value for this property.

5. __call

ans – whenever , we need to access the unaccessible methods , actually in php , overloading is not support .

i think , you would reliazed that , when you define same name two methods in same page , it will be throw fatal error

 

like –

 

class abc

{

test (a){

// code goes here

}

 

test(a,b)

{

// code goes here

}

}

 

it will be not execute , it throws fatal error in php .

 

so __call methods come to resolves this issue  . i am going to explain you .what  is really __call magic methods .

 

class Animals {

  public function __construct($id) {
    $this->lions($id);
  }

  public function lions($id) {
    //  code goes here
  }

  public function __get($name) {
    if($name == 'name') {
      return $this->username;
    }
  }

  public function __set($field, $value) {
    if($field == 'name') {
      $this->username = $value;
    }
  }

  public function __call($method, $arg) {
      echo "unknown method " . $method;
      return false;
  }
}

 

 

 

6 .  __sleep

7. __wakeup

8. __clone

9. __toString

10. __autoloading

ans –      It is automatically called the class file , whenever we create object of the class.

EX –

__autoloading($classname)

{

include(‘class/$classname’);

}

$obj1 = new ABC();

$obj2 = new DEF();

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *