Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

List all classes inherited from

Posted on 2014-04-08
4
Medium Priority
?
208 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
  • 3
4 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

886 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