3

If I have class:

class_A{
    use SomeTrait;
}

And

class_B extends class_A{
    //
}

How to disable trait "SomeTrait" in class_B class ?

1
  • 2
    There's not a way to disable it - you can override the methods in it, or simply 'not use them', but you can't undo it. Commented Feb 15, 2016 at 15:53

2 Answers 2

1

You can't disable inheriting trait in a subclass.

However you can change trait's method visibility.

Sign up to request clarification or add additional context in comments.

Comments

0

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();
    
    

2 Comments

Extending is still valid (even when using traits) - for example, you can't type-hint on a trait
extending is still valid.. no 5hit .. !?! You have opened my eyes.. ;) Typehinting, yes, not possible with traits, but.. in this question, is there any word about type-hinting in first place?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.