Just to clarify as it seems to happen to a lot of people, there's two possible cases.
Let's use the class `Bar` for all those cases:
class Bar {
public function __construct(
\First\Argument $first,
\Second\Argument $second
) {
// Constructor code
}
}
NB: please note that in Magento 2, the `$data` array parameter always comes last: http://magento.stackexchange.com/q/118033/2380
## Case 1 ##
You have class `Foo` that extends `Bar`
class Foo extends Bar {
public function __construct(
\First\Argument $first,
\Second\Argument $second,
\Test $test,
\Testtwo $testtwo
) {
parent::__construct($second, $first);
}
}
**The problem** : in that case, the call to the parent constructor does not provide the arguments in the right order. To fix that you must match the parent constructor:
parent::__construct($first, $second);
## Case 2 ##
You have class `Foo` that extends `Bar`
class Foo extends Bar {
public function __construct(
\First\Argument $first,
\Second\Argument $second,
\Test $test,
\Testtwo $testtwo
) {
parent::__construct($first, $second);
}
}
I hear you saying:
> What the hell is wrong with this one ?
Well, the answer is : nothing!
Even if that class looks okay you might get the `Uncaught TypeError: Argument X passed to Class must be an instance of Class2` error because of the Magento 2 class generation system.
To fix that, simply delete `var/generation` folder
Extra Case
----------
You have class Foo that extends Bar
class Foo extends Bar {
public function __construct(
\Test $test,
\Testtwo $testtwo,
\First\Argument $first,
\Second\Argument $second
) {
parent::__construct($first, $second);
}
}
In that particular case, [the static tests will complain about the order of parameters in the constructor][1]. To fix that you should change:
public function __construct(
\First\Argument $first,
\Second\Argument $second,
\Test $test,
\Testtwo $testtwo
) {
parent::__construct($first, $second);
}
[1]: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Code/Validator/ArgumentSequence.php#L82