'PHP How to disable trait in subclass?

If I have class:

class_A{
    use SomeTrait;
}

And

class_B extends class_A{
    //
}

How to disable trait "SomeTrait" in class_B class ?



Solution 1:[1]

You can't disable inheriting trait in a subclass.

However you can change trait's method visibility.

Solution 2:[2]

Why extending class in first place when using traits - if - *(let's say it's true) there are A LOTS of traits in Your code/project .. ?


    class A {
        use private_trait_holding_this,
            private_trait_holding_just_that;
       
        // .. properties, public methods...
    }
    
    class B {
        use private_trait_holding_just_that;
    
        // ... properties, public methods ....
    }

Traits are very powerful things, and I often like to refer to them as bags. Just because of this below. Note that everything inside traits is private.

    
        trait private_properties {
    
          private $path;
          private $file_;
          private $pack_;
        }
    
        trait private_methods_uno
        {
    
          private function getFilePrivate()
          {
               if(is_file($this->path))
               $this->file_ = file_get_contents($this->path);
          }
          
        }
    
        trait private_methods_due
        {
    
          private function putFilePrivate()
          {
               if(!is_string($this->file_))
                    die('File probably doesn\'t exist ... ');
               else
               {
                    $this->pack_ = base64_encode($this->file_);
                    file_put_contents(("{$this->path}.bak"), $this->pack_, LOCK_EX);
                    $this->pack_ = null; $this->file_ = $this->pack_;
               }
          }
          
        }
    
        final class opcodeToString
        {
          use
               private_properties,
               private_methods_uno,
               private_methods_due;
    
          public function __construct($path)
          {
               $this->path = $path;
               $this->getFilePrivate();
          }
    
          public function putFile()
          {
               $this->putFilePrivate();
          }
    
        }
    
    
    
        $filepath = '/my/path/to/opcode.php';
        $ots = new opcodeToString($filepath);
        $ots->putFile();
    
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 martin
Solution 2