jordanwb Posted August 11, 2008 Share Posted August 11, 2008 Let's say I have this class: abstract class Theme { private static $themes; public static function AddThemeCreator ($name, $reference_to_theme) { Theme::$themes[$name] = $reference_to_theme; } public static function LoadTheme ($name) { return new Theme::$themes[$name] (); } } class SevenAreaTheme extends Theme { public function __construct () { } } Theme::AddThemeCreator ('SevenAreaTheme', AddThemeCreator); When php Passes "SevenAreaTheme" to Theme::LoadTheme, it returns an instance of the SevenAreaTheme class. I know you could do this in .Net with delegates, but it there something like this for PHP? Quote Link to comment https://forums.phpfreaks.com/topic/119204-solved-passing-a-reference-to-a-function/ Share on other sites More sharing options...
Daniel0 Posted August 11, 2008 Share Posted August 11, 2008 Maybe what you're looking for is the factory pattern? http://en.wikipedia.org/wiki/Factory_method_pattern Quote Link to comment https://forums.phpfreaks.com/topic/119204-solved-passing-a-reference-to-a-function/#findComment-614018 Share on other sites More sharing options...
jordanwb Posted August 11, 2008 Author Share Posted August 11, 2008 Yes that's what I'm looking for. I don't want to use switches because I want the code to be dynamic. For example in the database there's a settings table which has a field called "DefaultTheme", this contains a string which could contain the value "SevenAreaTheme", the script gets the value from the table, passes it to Theme::LoadTheme, and returns an instance of that theme. Quote Link to comment https://forums.phpfreaks.com/topic/119204-solved-passing-a-reference-to-a-function/#findComment-614032 Share on other sites More sharing options...
Daniel0 Posted August 11, 2008 Share Posted August 11, 2008 How about this? It would be a factory implementation. abstract class Theme { static public function factory($type) { $class = $type . 'Theme'; if (!class_exists($class)) { throw new Exception("Theme doesn't exist"); } return new $class; } } class SevenAreaTheme extends Theme { // bla bla } Quote Link to comment https://forums.phpfreaks.com/topic/119204-solved-passing-a-reference-to-a-function/#findComment-614034 Share on other sites More sharing options...
jordanwb Posted August 11, 2008 Author Share Posted August 11, 2008 Sweet, that works. I didn't think that something like that would work, but it does. One question, would that be considered bad style? I mean the "return new $class;" part. Quote Link to comment https://forums.phpfreaks.com/topic/119204-solved-passing-a-reference-to-a-function/#findComment-614053 Share on other sites More sharing options...
Daniel0 Posted August 11, 2008 Share Posted August 11, 2008 Not at all. Otherwise I would've never suggested it Quote Link to comment https://forums.phpfreaks.com/topic/119204-solved-passing-a-reference-to-a-function/#findComment-614059 Share on other sites More sharing options...
jordanwb Posted August 11, 2008 Author Share Posted August 11, 2008 Fair Enough. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/119204-solved-passing-a-reference-to-a-function/#findComment-614077 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.