note 46634 added to language.oop5.reflection

From: Date: Mon, 18 Oct 2004 16:40:38 +0000
Subject: note 46634 added to language.oop5.reflection
Groups: php.notes 
Request: Send a blank email to php-notes+get-78727@lists.php.net to get a copy of this message
If you've ever wanted to do dynamic class loading in PHP5, especially when the class you're trying to dynamically load is a Singleton (and therefore you cannot use the new operator), you can do something like this example below, using the PHP5 Reflection API: <?php abstract class Shape { static public function makeShape( $shapeName ) { $shapeInstance = null; $shapeClass = new ReflectionClass( $shapeName ); $shapeMethod = $shapeClass->getMethod( 'getInstance' ); $shapeInstance = $shapeMethod->invoke( null ); $shapeClass = null; $shapeMethod = null; return $shapeInstance; } abstract public function doStuff(); } class Triangle extends Shape { private static $instance = null; private function __construct() { } public static function getInstance() { if ( null == self::$instance ) { self::$instance = new self; } return self::$instance; } public function doStuff() { } } $typeOfShape = 'Triangle'; $shape = null; try { $shape = Shape::makeShape( $typeOfShape ); } catch ( Exception $e ) { print "Error creating shape '$typeOfShape'! " . $e->getMessage() . "\n"; } if ( null != $shape ) { // $shape will be an instance of Triangle $shape->doStuff(); } ?> So by changing the value of $typeOfShape you can dynamically load the appropriate Shape subclass at runtime, thus facilitating a sort of plug-in style architecture for your classes. You can just drop in new Shape subclasses and not have to modify any of the Shape class code to support them in its factory method makeShape() :-) If your subclasses are all in separate files, you could even make the 'including' of these files dynamic as well, by adding these lines to the Shape::makeShape() method after the $shapeInstance is initialized: <?php ini_set( 'include_path', ini_get( 'include_path' ) . PATH_SEPARATOR . '/path/to/your/php/class/include/files' ); /** * Assuming your subclasses are in files called 'class_$shapeName.php' :-) * Of course doing a dynamic require() could be a security problem depending * on how you validate/clean your $shapeName method parameter (if at all ;-)) */ require_once( "class_$shapeName.php" ); ?> ---- Manual Page -- http://www.php.net/manual/en/language.oop5.reflection.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46634 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46634&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46634&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46634&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46634&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46634&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46634&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46634&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78727) next »