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? 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 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. 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 } 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. 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 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. 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
Archived
This topic is now archived and is closed to further replies.