Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3793526
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:47:21+00:00 2026-05-19T12:47:21+00:00

I have a PHP object that consists of a set of classes. For sake

  • 0

I have a PHP object that consists of a set of classes. For sake of simplicity lets call it an object of class C that extends class B which in its turn extends class A. At some point in my code I want to clean up the object by calling its doCleanup() function which it inherits from interface I:

interface I { public function doCleanup(); }

class A implements I { ... }
class B extends A { ... }
class C extends B implements I { ... }

In the doCleanup function in class C I want to also execute any cleanup function in my parent classes (in this case, the doCleanup() in class A). However, for some objects I am not sure whether any of the parent classes actually implement interface I, so I am not sure whether I can simpley call parent::doCleanup().

My question therefore is if there is a way to check whether any of my ancestors implement the interface for example by using some sort of instanceof call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:47:21+00:00Added an answer on May 19, 2026 at 12:47 pm

    You can do this nicely with get_parent_class and is_subclass_of (which works for interfaces as well as parent classes):

    <?php
    
    interface I {
        public function doCleanup();
    }
    
    class A implements I {
        public function doCleanup() {
            echo "done cleanup\n";
        }
    }
    
    class B extends A {}
    
    class C extends B implements I {
        public function doCleanup() {
            if (is_subclass_of(get_parent_class($this), 'I')) {
                parent::doCleanup();
            }
        }
    }
    
    $c = new C;
    $c->doCleanup(); // outputs "done cleanup"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my own php data object class that wraps the mysqli library. It
If I have in my php app an object that connects to the database,
I have a need to create a library of Object Oriented PHP code that
I have a JSON object which is generated by PHP. It's an object with
I have a tree-like object structure that consists of two types of objects: object
I found out that object constants in PHP always have public visibility so it
I have a html page that calls a php object to get some data
My searching skills seems to have failed me. I have this php object that
I have a $database object that I need to reference inside another class. Would
I have a Model class for a Generic Object that contains an array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.