Solved

List all classes inherited from

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

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Thank you
0
 
LVL 108

Expert Comment

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

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now