PHP how to use trait from parent class inside of trait [duplicate] - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, April 12, 2023

PHP how to use trait from parent class inside of trait [duplicate]

Is it possible to inherit a trait from another trait?

For example:

class A {
     use AB;
     use AC;
}

trait AB {
public function DoSome1() {
     echo 'hello1';

     // I want to echo out AC here
     // Will not work due to error: class declarations cannot be nested
     class NewClass {
          use AC;
     }
     $Display = new NewClass;

     // HOW TO DISPLAY LIKE THIS
     $Display->DoSome2();
}

trait AC {
public function DoSome2() {
     echo 'hello2';
}

$Display = new A;

// Outputs normal hello1
$Display->AB();

I want to include trait AC inside of AB. Is that possible?



source https://stackoverflow.com/questions/75988723/php-how-to-use-trait-from-parent-class-inside-of-trait

No comments:

Post a Comment