I’m wondering if there is a “better” way to do this:
class Foo {
final public function bar() {
if (is_subclass_of(get_called_class(), __CLASS__)) {
throw new Exception();
}
}
}
class Bar extends Foo {
public function baz() {
parent::bar(); // shouldn't be allowed
}
}
Essentially, I want certain methods in my parent class to prohibit child classes from calling them. This needs to be bullet-proof, which I doubt this is, so if you know how this could be circumvented, that’s what I’m interested in knowing (along with how to prevent it, if possible).
Edit: For everyone suggesting private methods, this is not an option, as I need the interface to remain public to be externally accessible. Sorry, I guess I assumed that would be obvious.
1 Answer