Jump to content

[SOLVED] Passing a reference to a function


jordanwb

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
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.