Reputation: 422
Apologies if this is a duplicate, I did search for the answer previously.
I'm struggling to overload a method defined in a trait. It throws a fatal error:
Fatal error: Configuration has colliding constructor definitions coming from traits in Configuration.php on line 18
Their Class
<?php
namespace Theirs\Package;
use Theirs\TheirTrait;
class Configration
{
use TheirTrait;
}
My Class
<?php
namespace My\Package;
use Theirs\Package\Configuration as BaseConfiguration;
use My\Trait\MyTrait;
class Configuration extends BaseConfiguration
{
use MyTrait;
}
My Trait
use Theirs\TheirTrait as BaseSomeTrait;
trait MyTrait
{
use BaseSomeTrait;
protected function someMethod($something)
{
// ...
}
}
Upvotes: 2
Views: 1600
Reputation: 12721
you can resolve the constructor collision like this.
trait MyTrait {
use BaseSomeTrait{
BaseSomeTrait::__construct as private __otherConstruct;
}
public function __construct(/* maybe params here*/)
{
// maybe other code
$this->__otherConstruct(/* maybe params here*/);
// maybe other code
}
}
if MyClass has a constructor as well you need to do it there additionally, or maybe only there if MyTrait has no constructor...
Upvotes: 7
Reputation: 13107
It looks like both Theirs\TheirTrait\Configration
and Theirs\Package\Configuration
have a constructor, and the constructor signatures are incompatible. (But without the code of both, it’s hard to tell.)
If Theirs\Package\Configuration
is really intended to be used with Theirs\TheirTrait\Configration
, you should write them a bug report. Otherwise, I guess there should be some documentation on how to use the class and the trait in your code so that they don’t produce errors.
Upvotes: 0