How to Get the Sub Classes of a Parent Class

How to Get the Sub Classes of a Parent Class

am 25.10.2009 04:07:13 von Raymond Irving

--0-187218677-1256440033=:26637
Content-Type: text/plain; charset=us-ascii

Hello,

Does any know of an optimized solution to get all the subclasses for a parent class?

__
Raymond Irving

--0-187218677-1256440033=:26637--

Re: How to Get the Sub Classes of a Parent Class

am 25.10.2009 14:44:40 von David Otton

2009/10/25 Raymond Irving :

> Hello,
>
> Does any know of an optimized solution to get all the subclasses for a parent class?

That's not exactly a typical thing to want, although I can see a
couple of ways to do it... what exactly are you trying to do, and are
you using PHP5.3?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to Get the Sub Classes of a Parent Class

am 25.10.2009 15:16:28 von Raymond Irving

--0-1509758098-1256480188=:22992
Content-Type: text/plain; charset=us-ascii

I want to automatically initialize a specific sub class when the php page is loaded.

I'm looking for a solution for php 5.1+ or anything that's optimized for 5.3



________________________________
From: David Otton
To: Raymond Irving
Cc: php-general@lists.php.net
Sent: Sun, October 25, 2009 8:44:40 AM
Subject: Re: [PHP] How to Get the Sub Classes of a Parent Class

2009/10/25 Raymond Irving :

> Hello,
>
> Does any know of an optimized solution to get all the subclasses for a parent class?

That's not exactly a typical thing to want, although I can see a
couple of ways to do it... what exactly are you trying to do, and are
you using PHP5.3?

--0-1509758098-1256480188=:22992--

Re: How to Get the Sub Classes of a Parent Class

am 25.10.2009 16:25:27 von David Otton

2009/10/25 Raymond Irving :

> I want to automatically initialize a specific sub class when the php page is loaded.
>
> I'm looking for a solution for php 5.1+ or anything that's optimized for 5.3

You may be able solve this with a simple class_exists() (pseudo-code ahead):

if(class_exists($var)) {
$class = new $var;
} else {
$class = new FourOhFour;
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to Get the Sub Classes of a Parent Class

am 27.10.2009 02:22:29 von Raymond Irving

--0-1700575797-1256606549=:99806
Content-Type: text/plain; charset=us-ascii


This works if you know the name of the class. What I'm looking for is a way to get one of the sub classes and initialize it dynamically.




________________________________
From: David Otton
To: Raymond Irving
Cc: PHP-General List
Sent: Sun, October 25, 2009 10:25:27 AM
Subject: Re: [PHP] How to Get the Sub Classes of a Parent Class

2009/10/25 Raymond Irving :

> I want to automatically initialize a specific sub class when the php page is loaded.
>
> I'm looking for a solution for php 5.1+ or anything that's optimized for 5.3

You may be able solve this with a simple class_exists() (pseudo-code ahead):

if(class_exists($var)) {
$class = new $var;
} else {
$class = new FourOhFour;
}

--0-1700575797-1256606549=:99806--

Re: How to Get the Sub Classes of a Parent Class

am 27.10.2009 02:33:02 von David Otton

2009/10/27 Raymond Irving :

>>> I want to automatically initialize a specific sub class when the php page
>>> is loaded.

>> You may be able solve this with a simple class_exists() (pseudo-code ahead):
>>
>> if(class_exists($var)) {
>> $class = new $var;
>> } else {
>> $class = new FourOhFour;
>> }

>> This works if you know the name of the class. What I'm looking for is a way
>> to get one of the sub classes and initialize it dynamically.

I'm confused. If you don't know the name of the child class, how are
you going to know which one to instantiate? Are you going to pick one
at random?

What you're trying to do can probably be accomplished, but I can't
shake the feeling we're trying to solve the wrong problem here.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to Get the Sub Classes of a Parent Class

am 27.10.2009 02:34:05 von Martin Scotta

--00032556054a7c24fa0476e0ac77
Content-Type: text/plain; charset=ISO-8859-1

On Mon, Oct 26, 2009 at 10:22 PM, Raymond Irving wrote:

>
> This works if you know the name of the class. What I'm looking for is a way
> to get one of the sub classes and initialize it dynamically.
>
>
>
>
> ________________________________
> From: David Otton
> To: Raymond Irving
> Cc: PHP-General List
> Sent: Sun, October 25, 2009 10:25:27 AM
> Subject: Re: [PHP] How to Get the Sub Classes of a Parent Class
>
> 2009/10/25 Raymond Irving :
>
> > I want to automatically initialize a specific sub class when the php page
> is loaded.
> >
> > I'm looking for a solution for php 5.1+ or anything that's optimized for
> 5.3
>
> You may be able solve this with a simple class_exists() (pseudo-code
> ahead):
>
> if(class_exists($var)) {
> $class = new $var;
> } else {
> $class = new FourOhFour;
> }
>

This script only works for loaded classes .
If your script has autoloader then there is no way to know the declared
classes before you declare them.

error_reporting( E_ALL | E_STRICT );

Class Foo {}

Class Bar extends Foo {}
Class Baz extends Foo {}

Class Beer extends Bar {}
Class Pier extends Bar {}

function get_subclasses($class)
{

$sub = array();

foreach(get_declared_classes() as $name )
{
$rClass = new ReflectionClass($name);

if( $rClass->isSubclassOf( $class ))
{
$sub[] = $name;
}
}

return $sub;
}

print_r( get_subclasses( 'Foo' ));
print_r( get_subclasses( 'Bar' ));

--
Martin Scotta

--00032556054a7c24fa0476e0ac77--

Re: How to Get the Sub Classes of a Parent Class

am 27.10.2009 02:48:12 von Martin Scotta

--0015175d07acf7aafb0476e0def7
Content-Type: text/plain; charset=ISO-8859-1

On Mon, Oct 26, 2009 at 10:33 PM, David Otton <
phpmail@jawbone.freeserve.co.uk> wrote:

> 2009/10/27 Raymond Irving :
>
> >>> I want to automatically initialize a specific sub class when the php
> page
> >>> is loaded.
>
> >> You may be able solve this with a simple class_exists() (pseudo-code
> ahead):
> >>
> >> if(class_exists($var)) {
> >> $class = new $var;
> >> } else {
> >> $class = new FourOhFour;
> >> }
>
> >> This works if you know the name of the class. What I'm looking for is a
> way
> >> to get one of the sub classes and initialize it dynamically.
>
> I'm confused. If you don't know the name of the child class, how are
> you going to know which one to instantiate? Are you going to pick one
> at random?
>
> What you're trying to do can probably be accomplished, but I can't
> shake the feeling we're trying to solve the wrong problem here.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Just having FUN, do not take me serious : )

function getInstanceRandom()
{
$classes = get_declared_classes();
$index = array_rand( $classes );
return new $classes[ $index ]();
}

$obj = getInstanceRandom();
echo get_class( $obj );

This is way you NEVER know which class you will instantiate!

--
Martin Scotta

--0015175d07acf7aafb0476e0def7--

Re: How to Get the Sub Classes of a Parent Class

am 28.10.2009 01:25:42 von Raymond Irving

--0-1644438948-1256689542=:64423
Content-Type: text/plain; charset=us-ascii

David,

I'm try to do something like what Martin Scotta did but I'm looking for a solution that did not require me to loop through get_declared classes() to find a sub class.

__
Raymond Irving





________________________________
From: David Otton
To: Raymond Irving
Cc: PHP-General List
Sent: Mon, October 26, 2009 8:33:02 PM
Subject: Re: [PHP] How to Get the Sub Classes of a Parent Class

2009/10/27 Raymond Irving :

>>> I want to automatically initialize a specific sub class when the php page
>>> is loaded.

>> You may be able solve this with a simple class_exists() (pseudo-code ahead):
>>
>> if(class_exists($var)) {
>> $class = new $var;
>> } else {
>> $class = new FourOhFour;
>> }

>> This works if you know the name of the class. What I'm looking for is a way
>> to get one of the sub classes and initialize it dynamically.

I'm confused. If you don't know the name of the child class, how are
you going to know which one to instantiate? Are you going to pick one
at random?

What you're trying to do can probably be accomplished, but I can't
shake the feeling we're trying to solve the wrong problem here.

--0-1644438948-1256689542=:64423--

Re: How to Get the Sub Classes of a Parent Class

am 28.10.2009 01:30:28 von Raymond Irving

--0-601102942-1256689828=:66586
Content-Type: text/plain; charset=us-ascii

Hi Martin,

This works great but I was hoping that I didn't have to loop through get_declared_classes to find the sub class.

Is there a way to get the subclasses using Reflection? For example:

$r = new ReflectionClass($name);
print_r($r->getSubClasses());

Many Thanks




________________________________
From: Martin Scotta
To: Raymond Irving
Cc: David Otton ; PHP-General List
Sent: Mon, October 26, 2009 8:34:05 PM
Subject: Re: [PHP] How to Get the Sub Classes of a Parent Class



On Mon, Oct 26, 2009 at 10:22 PM, Raymond Irving wrote:


>>This works if you know the name of the class. What I'm looking for is a way to get one of the sub classes and initialize it dynamically.
>
>
>
>
>
>>________________________________
>>From: David Otton
>To: Raymond Irving
>>Cc: PHP-General List
>>Sent: Sun, October 25, 2009 10:25:27 AM
>
>Subject: Re: [PHP] How to Get the Sub Classes of a Parent Class
>
>
>2009/10/25 Raymond Irving :
>
>>> I want to automatically initialize a specific sub class when the php page is loaded.
>>>
>>> I'm looking for a solution for php 5.1+ or anything that's optimized for 5.3
>
>>You may be able solve this with a simple class_exists() (pseudo-code ahead):
>
>>if(class_exists($var)) {
>> $class = new $var;
>>} else {
>> $class = new FourOhFour;
>>}

This script only works for loaded classes .
If your script has autoloader then there is no way to know the declared classes before you declare them.

error_reporting( E_ALL | E_STRICT );

Class Foo {}

Class Bar extends Foo {}
Class Baz extends Foo {}

Class Beer extends Bar {}
Class Pier extends Bar {}

function get_subclasses($class)
{

$sub = array();

foreach(get_declared_classes() as $name )
{
$rClass = new ReflectionClass($name);

if( $rClass->isSubclassOf( $class ))
{
$sub[] = $name;
}
}

return $sub;
}

print_r( get_subclasses( 'Foo' ));
print_r( get_subclasses( 'Bar' ));

--
Martin Scotta

--0-601102942-1256689828=:66586--

Re: How to Get the Sub Classes of a Parent Class

am 28.10.2009 02:27:48 von Martin Scotta

--000325559e26d6b9d30476f4b3d2
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Oct 27, 2009 at 9:30 PM, Raymond Irving wrote:

> Hi Martin,
>
> This works great but I was hoping that I didn't have to loop through
> get_declared_classes to find the sub class.
>
> Is there a way to get the subclasses using Reflection? For example:
>
> $r = new ReflectionClass($name);
> print_r($r->getSubClasses());
>
> Many Thanks
>
> ------------------------------
> *From:* Martin Scotta
>
> *To:* Raymond Irving
> *Cc:* David Otton ; PHP-General List <
> php-general@lists.php.net>
> *Sent:* Mon, October 26, 2009 8:34:05 PM
>
> *Subject:* Re: [PHP] How to Get the Sub Classes of a Parent Class
>
>
> On Mon, Oct 26, 2009 at 10:22 PM, Raymond Irving wrote:
>
>>
>> This works if you know the name of the class. What I'm looking for is a
>> way to get one of the sub classes and initialize it dynamically.
>>
>>
>>
>>
>> ________________________________
>> From: David Otton
>> To: Raymond Irving
>> Cc: PHP-General List
>> Sent: Sun, October 25, 2009 10:25:27 AM
>> Subject: Re: [PHP] How to Get the Sub Classes of a Parent Class
>>
>> 2009/10/25 Raymond Irving :
>>
>> > I want to automatically initialize a specific sub class when the php
>> page is loaded.
>> >
>> > I'm looking for a solution for php 5.1+ or anything that's optimized for
>> 5.3
>>
>> You may be able solve this with a simple class_exists() (pseudo-code
>> ahead):
>>
>> if(class_exists($var)) {
>> $class = new $var;
>> } else {
>> $class = new FourOhFour;
>> }
>>
>
> This script only works for loaded classes .
> If your script has autoloader then there is no way to know the declared
> classes before you declare them.
>
> error_reporting( E_ALL | E_STRICT );
>
> Class Foo {}
>
> Class Bar extends Foo {}
> Class Baz extends Foo {}
>
> Class Beer extends Bar {}
> Class Pier extends Bar {}
>
> function get_subclasses($class)
> {
>
> $sub = array();
>
> foreach(get_declared_classes() as $name )
> {
> $rClass = new ReflectionClass($name);
>
> if( $rClass->isSubclassOf( $class ))
> {
> $sub[] = $name;
> }
> }
>
> return $sub;
> }
>
> print_r( get_subclasses( 'Foo' ));
> print_r( get_subclasses( 'Bar' ));
>
> --
> Martin Scotta
>


Hi Raymon

I think your main problem here is that you do not know which class you will
instantiate.
This can lead to undefined behaviour.

Maybe you need to re-analyse your solution.

cheers,
Martin Scotta

--000325559e26d6b9d30476f4b3d2--

Re: How to Get the Sub Classes of a Parent Class

am 28.10.2009 10:04:33 von David Otton

2009/10/28 Raymond Irving :

> =A0I'm try to do something like what Martin Scotta did but I'm looking fo=
r a
> solution that did not require me to loop through get_declared classes() t=
o
> find a sub class.

Place all your child classes in the same namespace, and use
ReflectionNamespace::getClasses().

But I stress, again, that I think there's a design issue that needs to
be fixed here. There's going to be a better way to do what you're
trying to do.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to Get the Sub Classes of a Parent Class

am 28.10.2009 10:14:55 von David Otton

2009/10/28 David Otton :
> 2009/10/28 Raymond Irving :
>
>> =A0I'm try to do something like what Martin Scotta did but I'm looking f=
or a
>> solution that did not require me to loop through get_declared classes() =
to
>> find a sub class.
>
> Place all your child classes in the same namespace, and use
> ReflectionNamespace::getClasses().

Hah. Turns out that's on the @todo list... wait for PHP 5.4 maybe.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php