Jump to content

Recommended Posts

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?

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.

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
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.