Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instanceof error in php wrapper class

Tags:

oop

php

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.

like image 920
xan Avatar asked Jul 24 '13 23:07

xan


1 Answers

You have defined

class phpclass1 :: addDoc(phpclass2 $string)

This method expects the argument to be an object of phpclass2, but you are passing

return $this->conn->addDoc($document);

through

$conn->add($doc);

and $doc is an object of wrapper2 not phpclass2

To fix add a new public method

wrapper2::getDoc()

public function add(wrapper2 $document) {
    return $this->conn->addDoc($document->getDoc());
}
like image 184
Michal M. Avatar answered Oct 23 '22 03:10

Michal M.