D
D
Dmitry2018-02-25 00:18:24
PHP
Dmitry, 2018-02-25 00:18:24

How to use trait in child class?

Hello!
There is a code:

trait SomeTrait
{
    public function someMethod() { echo 'Running trait method', PHP_EOL; }
}

class Foo
{
    use SomeTrait;
}

class Child extends Foo
{
    
    public function someMethod2() {
      echo 'Running class method', PHP_EOL;
      parent::someOtherMethod();
    }
}

$ch = new Child;
$ch->someMethod2();

In general, you need to get the value of the trait - SomeTrait

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2018-02-25
@muldy

$this

R
Rodion, 2018-02-25
@rodion11

public function someMethod2() 
{
      echo 'Running class method', PHP_EOL;
      $this->someMethod();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question