Re: [PHP] How to Get the Sub Classes of a Parent Class
2009/10/28 Raymond Irving xwis...@yahoo.com: 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. 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: [PHP] How to Get the Sub Classes of a Parent Class
2009/10/28 David Otton phpm...@jawbone.freeserve.co.uk: 2009/10/28 Raymond Irving xwis...@yahoo.com: 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. 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
Re: [PHP] How to Get the Sub Classes of a Parent Class
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 phpm...@jawbone.freeserve.co.uk To: Raymond Irving xwis...@yahoo.com Cc: PHP-General List php-general@lists.php.net 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 xwis...@yahoo.com: 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.
Re: [PHP] How to Get the Sub Classes of a Parent Class
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 martinsco...@gmail.com To: Raymond Irving xwis...@yahoo.com Cc: David Otton phpm...@jawbone.freeserve.co.uk; 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 xwis...@yahoo.com 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 phpm...@jawbone.freeserve.co.uk To: Raymond Irving xwis...@yahoo.com Cc: PHP-General List php-general@lists.php.net 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 xwis...@yahoo.com: 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
Re: [PHP] How to Get the Sub Classes of a Parent Class
On Tue, Oct 27, 2009 at 9:30 PM, Raymond Irving xwis...@yahoo.com 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 martinsco...@gmail.com *To:* Raymond Irving xwis...@yahoo.com *Cc:* David Otton phpm...@jawbone.freeserve.co.uk; 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 xwis...@yahoo.comwrote: 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 phpm...@jawbone.freeserve.co.uk To: Raymond Irving xwis...@yahoo.com Cc: PHP-General List php-general@lists.php.net 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 xwis...@yahoo.com: 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
Re: [PHP] How to Get the Sub Classes of a Parent Class
2009/10/25 Raymond Irving xwis...@yahoo.com: 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: [PHP] How to Get the Sub Classes of a Parent Class
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 phpm...@jawbone.freeserve.co.uk To: Raymond Irving xwis...@yahoo.com 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 xwis...@yahoo.com: 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?
Re: [PHP] How to Get the Sub Classes of a Parent Class
2009/10/25 Raymond Irving xwis...@yahoo.com: 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
[PHP] How to Get the Sub Classes of a Parent Class
Hello, Does any know of an optimized solution to get all the subclasses for a parent class? __ Raymond Irving