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
Solved

List all classes inherited from

Posted on 2014-04-08
4
200 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 109

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 109

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 109

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 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