php - Propogating a function to parents -
i have php object consists of set of classes. sake of simplicity lets call object of class c extends class b in turn extends class a. @ point in code want clean object calling docleanup() function inherits interface i:
interface { public function docleanup(); } class implements { ... } class b extends { ... } class c extends b implements { ... }
in docleanup function in class c want execute cleanup function in parent classes (in case, docleanup() in class a). however, objects not sure whether of parent classes implement interface i, not sure whether can simpley call parent::docleanup()
.
my question therefore if there way check whether of ancestors implement interface example using sort of instanceof
call?
you can nicely get_parent_class
, is_subclass_of
(which works interfaces parent classes):
<?php interface { public function docleanup(); } class implements { public function docleanup() { echo "done cleanup\n"; } } class b extends {} class c extends b implements { public function docleanup() { if (is_subclass_of(get_parent_class($this), 'i')) { parent::docleanup(); } } } $c = new c; $c->docleanup(); // outputs "done cleanup"
Comments
Post a Comment