Looking for a better method to impliment the following...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Besnell
Forum Newbie
Posts: 1
Joined: Tue Apr 25, 2006 5:31 pm

Looking for a better method to impliment the following...

Post by Besnell »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey everyone.  Shown below is a factory method I have in one of my classes.  Each class that is getting dynamically loaded has a getInstance() method.  One workaround I found was to use the eval() function.  I have found however that the eval() function seems to be extremely slow.  Can anyone provide any thoughts idea's on implimenting what I am doing without the use of the eval function?

Code: Select all

public static function getDAO($type) {
		if (include_once('DAO/' . $type . '.php')) {
			$type = $type . 'DAO';
			if (class_exists($type)) {
				try {
					$class = null;
					$eval = '$class = ' . $type . '::getInstance();';
					eval($eval);
					if ($class instanceof DA ) {
						return $class;
					}
				} catch (Exception $error) {
					return PEAR::raiseError($error->getMessage());
				}
			}
			return PEAR::raiseError('Class: ' . $type . ' coult not be found.');
		}
		return PEAR::raiseError('File not found: ' . $type . '.php');
	}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$class = call_user_func(array($type, 'getInstance'));
Post Reply