Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP OOP get all parent classes of a class

Tags:

oop

php

class

I have class Mercedes and it has parent Car and Car has parent Main.
I want to get all parents from Mercedes Class

Mercedes extends Car 
Car extends Main

output should be something like this mercedes<-Car<-Main

like image 261
Anri Avatar asked Dec 18 '25 14:12

Anri


1 Answers

This would do:

function GetAllParents(instance) {
   return get_class(instance) . '<-' .
          implode('<-', array_reverse(class_parents(instance)));
}

Outputs all in the right order:

Mercedes<-Car<-Main

See documentation:

  • http://php.net/class_parents
  • http://php.net/implode
like image 161
Barry Staes Avatar answered Dec 21 '25 03:12

Barry Staes