How to implement a class?

I need to implement a class only one of its methods, no need to implement remaining methods, but missing methods gives errors saying I have to implement remaining. So how to implement a class only with one of methods?

It would probably help to add some context and code samples.

for example an interface class has methods A and B. I don’t need to implement method B but only A. but methods in interface class are not optional and all of them must be implemented. So if I just implement method A it gives error that I must implement remaining methods, e.g. method B. But I don’t need it. So how to implement only method A?

As @zookeeper said, without seeing some example code, I’m not sure how we can help.

This is a code smell, an indication of a deeper problem. If you don’t need method B, then your class shouldn’t be using an interface that requires method B.

So how to implement only method A?

Ignoring the code smell issue for a moment, you could define method B to just throw an exception.

public function methodB() { throw new Exception('Not implemented'); }
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.