PHP OOPs In Hindi – PHP Traits In Hindi

PHP

PHP OOPs In HindiPHP Traits In Hindi – Traits Ka Istemal PHP me Same Functionality Ko Bar Bar Na Repeat Karne Ke Liye Jata Hai |

Read Also – PHP Abstract Classes

For Example: Man Lijiye Aapko Class A, B, C Me Ek Function Chahiye Jo Output Me Hello World Print Karta Ho, To Uske Liye Hame Class A,B,C Me Alag Alag Code Likhna Padega Jisse Ki Hamari Code Ki Line Jyada Ho Jayengi.

To Isko Rukne Ke liye php traits Ka Istemal Kiya Jata Hai, Ise Ham Niche Example Ke Sath Dekhte Hai |

PHP Traits In Hindi

Trait Keyword in PHP Ka Istemal Karke Ham Kitne Bhi Functions Create Kar Sakte Hai And trait Function Ka Istemal Fir Ham Kisi Bhi Class me Kar Sakte Hai |

PHP Trait Ko Create Karne Ke Liye trait keyword Ka Istemal Kiya Jata Hai |

Example:

<?php
trait test {
  public function msg1() {
    echo "Hello World";
  }
}
?>

Aap Upper Dekh Sakte Hai Ki Hamne trait test Create Kar Liye Hai And trait test Ke Under Hamne Ek msg1 Function Create Kiya Hai |

Example 2:

<?php
trait test{
  public function msg1() {
    echo "hello World";
  }
}

class A{
  use test;
}
?>

Aap Example 2: Me Dekh Sakte Hai Ki Hamne trait test Create Kiya Hai And Trait test Ke Under Hamne Ek msg1 Function Create Kiya Hai |

Uske Bad Hamne Ek Class A Create Ki Hai And Uske Under use Keyword Ka Istemal Karke trait test Ko Use Kiya Hai |

Read Also – PHP Constants

Ab Ham Example 3 Me Class A Ka Ek object Create Karte Hai Or Fir Us Object Ke Through function msg1 Ko Call Karte Hai |

Example 3:

<?php
trait test{
  public function msg1() {
    echo "hello World";
  }
}

class A{
  use test;
}

$obj = new A();
$obj->msg1()
?>

Output:

hello World

Example 4:

<?php
trait message1 {
  public function msg1() {
    echo "OOP is fun! ";
  }
}

trait message2 {
  public function msg2() {
    echo "OOP reduces code duplication!";
  }
}

class A {
  use message1;
}

class B {
  use message1, message2;
}

$obj = new A();
$obj->msg1();
echo "<br>";

$obj2 = new B();
$obj2->msg1();
$obj2->msg2();
?>

Output:

OOP is fun!
OOP is fun! OOP reduces code duplication!

Leave a Reply

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