Solved

List all classes inherited from

Posted on 2014-04-08
4
201 Views
Last Modified: 2014-04-08
Is it possible to list all classes that have extended from class:-

So for example, I want to create a class called certificate, but there are several types of certificate some will override function from certificate some wont, so Ive set it up like:-
<?php
	class certificate {
		public function getDesciption() {
			return 'No description stored';
		}
	}













	class foo extends certicate {
		public function getDesciption() {
			return 'This is a Foo certificate';
	    	}
	}


	class bar extends certicate {
		public function getDesciption() {
			return 'This is a bar certificate';
		}
	}
?>

Open in new window


However if I want to list all the objects created from 'certificate' and call 'getDescription' from each, is there a method in PHP to list all objects created, and see what it extends from?

Thank you in advance
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 39985835
Maybe something like this?
http://php.net/manual/en/function.get-called-class.php

The question is kind of counterintuitive because your own PHP code will create the objects, so looking for a PHP function to tell you what objects you created doesn't make sense to me.  It would make more sense to keep an internal record of the classes used to instantiate the objects.  An array of objects is a very useful programming construct!
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39985873
See if this makes some sense.

<?php // demo/temp_tonelm54.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

class Certificate
{
    public function getDescription()
    {
        echo PHP_EOL . 'This is a ' .  get_class($this) . ' certificate';
    }

    public function whoAmI()
    {
        echo PHP_EOL . 'I am an Object Instance of ' .  get_class($this);
    }
}

class Foo extends certificate
{
    public function getDescription()
    {
        echo PHP_EOL . 'This is a ' .  get_class($this) . ' certificate';
    }
}

class Bar extends certificate
{
    public function getDescription()
    {
        echo PHP_EOL . 'This is a ' .  get_class($this) . ' certificate';
    }
}

echo '<pre>';
$c = new Certificate;
$f = new Foo;
$b = new Bar;

$c->getdescription();
$f->getdescription();
$b->getdescription();

$c->whoami();
$f->whoami();
$b->whoami();

Open in new window

0
 

Author Closing Comment

by:tonelm54
ID: 39986942
Thank you
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39987127
Glad to help.  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question