php - Determining source of method call at runtime -
php - Determining source of method call at runtime -
i'm wondering if there "better" way 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 allowed } }
essentially, want methods in parent class prohibit kid classes calling them. needs bullet-proof, uncertainty is, if know how circumvented, that's i'm interested in knowing (along how prevent it, if possible).
edit: suggesting private methods, not option, need interface remain public externally accessible. sorry, guess assumed obvious.
class foo { final public function bar() { if (is_subclass_of(get_called_class(), __class__)) { throw new exception('no cookies you!'); } echo 'failure!'; } } class bar extends foo { public function baz() { try{ foo::bar(); // shouldn't allowed } grab (exception $e){ echo $e->getmessage(); } try{ $func = function() {foo::bar();}; // allowed, nags should't called statically.. $func(); } grab (exception $e){ echo $e->getmessage(); } } } $b = new bar(); $b->baz();
php oop design
Comments
Post a Comment