Posts

Showing posts from September, 2024

Type Hinting in PHP OOPS

Image
  <?php //Type Hinting //Example 1 // function test(array $arr) // { //     echo "<table>"; //     foreach($arr as $k => $v) //     { //         echo "<tr><td>$k</td><td>$v</td></tr>"; //     } //     echo "</table>"; // } // $array = "hkhhlhlh"; // test($array); //Example 2 interface test {     public function doSomething (); } class ABC implements test {     public function doSomething ()     {         echo "Doing Something from ABC Class" ;     } } class XYZ implements test {     public function doSomething ()     {         echo "Doing Something from XYZ Class" ;     }     public function doSomethingElse ()     {         echo "Doing Something Else" ;     } } function test ( ABC $a...

namespaces in PHP OOPS

Image
  <?php // NOTE: Namespace declaration statement has to be the very first statement or after any declare call in the script // No code may exist outside of namespace {} namespace abc {     class xyz {} } namespace def {     class xyz     {         public function __construct ()         {             echo "XYZ from DEF namespace" ;         }     } } //If there is no Name present for namespace then it will consider as "Global namespace" as shown below: namespace {     class xyz     {         public function __construct ()         {             echo "XYZ from Global namespace" ;         }     }     $obj = new xyz ();     echo "<br><br>" ;     $objTwo = new def\ xyz ();  ...

Access Levels in Traits in PHP

Image
  <?php trait abc {     private function xyz ()     {         echo "XYZ method from trait abc" ;     } } class test {     use abc {         abc ::xyz as public;     //Changing the Access Modifier from private to public of Method xyz()         abc ::xyz as public abcXYZ;   //Changing the Access Modifier from private to public of method xyz()         // as well as Changing the name of method xyz() to abcXYZ()     } } $obj = new test (); $obj -> xyz (); echo "<br><br>" ; $obj -> abcXYZ ();

Handling Conflicts in Traits in PHP

Image
  <?php //When multiple trait have same named method then we can call both methods as shown below : trait one {     public function test ()     {         echo "TEST method from one trait." ;     } } trait two {     public function test ()     {         echo "TEST method from two trait." ;     } } class abc {     use one , two {         // one::test insteadof two;    //Here test() method in trait one will call         // two::test insteadof one;    //Here test() method in trait two will call         //Here below First of all, trait "two" method will call and then trait "one" method will call         two ::test insteadof one ;         one ::test as OneTest;         //Here below First of all, trait "one" method will call an...

Method Overriding in Traits in PHP

Image
  <?php class Base {     public function abc ()     {         echo "ABC method from Base class." ;     } } //The trait method(Here, abc() Method) will get priority compared to Parent class Method(Here, abc() Method) trait Test {     public function abc ()     {         echo "ABC method from test trait." ;     } } class Child extends Base {     use Test ;     public function abc ()     {         echo "ABC method from Child class." ;     } } $obj = new Child (); $obj -> abc ();