Solved

List all classes inherited from

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 dynamically set the form action using jQuery.

778 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