You told PHP to expect an instance of FetchMode
(exactly the same as the error message says), but FetchMode::FETCH*
passes the constant value. You will need to use some kind of Enum instance (which we don't have in PHP). (Well, there is SplEnum
, but who uses this?)) Or change the method signature to exclude typehint.
However, instead of a switch / case, you could solve this problem with polymorphism and a strategy template , for example, instead of doing something like
public function getRecordSet($mode) { switch ($mode) { case FetchMode::ALL:
which will increase the Cylcomatic Complexity of your class and force changes to this class and FetchMode
whenever you need to add additional FetchModes, you can do:
public function getRecordSet(FetchMode $fetchModeStrategy) { return $fetchModeStrategy->fetch(); }
and then interface protect option
interface FetchMode { public function fetch(); }
and add specific FetchMode
classes for each supported FetchMode
class FetchOne implements FetchMode { public function fetch() {
That way, you no longer have to touch the class with this getRecordSet
method, because it will work for any class that implements this FetchMode
inteface. Therefore, whenever you have new FetchModes, you simply add a new class, which in the end is much more convenient to maintain.
source share