I started of writing a wrapper class for a pre-defined php package. Here are the classes:
class phpclass1
:: ping()
:: __construct(array $options)
:: clear()
:: addDoc(phpclass2 $string)
...
class phpclass2
:: __construct()
:: update()
...
Here are the wrapper classes that I wrote for the above 2 classes:
class wrapper1 {
private $conn;
public function __construct(phpclass1 $object) {
$this->conn = $object;
}
public function add(wrapper2 $document) {
return $this->conn->addDoc($document);
}
}
class wrapper2 extends phpclass2 {
private $doc;
public function __construct() {
$this->doc = new phpclass2();
}
}
Here's how I'm using them:
$options = array (...);
$object = new phpclass1($options);
$conn = new wrapper1($object);
$doc = new wrapper2();
....
....
$conn->add($doc);
Everything was working until I used the add function. It gives an error: Argument 1 passed to phpclass1::addDoc() must be an instance of phpclass2, instance of wrapper2 given
What am I missing? I've tried out many things, but completely lost here.
phpclass1::addDoc()is type-hinted as aphpclass2, and you are passing it$document, which is awrapper2. Unlesswrapper2extendsphpclass2, the type hint will fail.finalorprivatemethods, you ought to be able to extend the class save for some potentially exotic case.