Skip to main content
added 56 characters in body
Source Link
class Base
{
    public $value = 1;
}

class Wire
{
    //List of classes I'll have to load
    private static $load = array("Foo", "Hello");

    //Template of the Bootstrap class
    private static $template = NULL;

    //Wire everything up
    public function Up()
    {
            //Get the bootstrap template
        self::GetTemplate();

            //Get each class
        foreach(self::$load as $class)
        {
                    //Load it...
            require_once "./class/" . $class . ".php";

                    //Build the static shell
            $bootstrap = str_replace("%CLASS%", $class, self::$template);
            eval($bootstrap);
        }
    }

    //Get the tempalte of the bootstrap class
    private function GetTemplate()
    {
        if(self::$template === NULL)
        {
                    //I've stored it in a separate file
            self::$template = file_get_contents("./class/Bootstrap.php");
        }
    }
}


Wire::Up();
//This is the Bootstrap class
//It offers a static shell for other instatiated classes
class %CLASS%
{
    public static $instance = NULL;
    public function __callStatic($method, $arguments)
    {
        if(self::$instance === NULL)
        {
            $class = get_called_class() . "Controller";
            self::$instance = new $class();
        }
        call_user_func_array(array(self::$instance, $method), $arguments);
    }
}

That's the Base class, for testing purpouses only

class Base
{
    public $value = 1;
}
class Base
{
    public $value = 1;
}

class Wire
{
    //List of classes I'll have to load
    private static $load = array("Foo", "Hello");

    //Template of the Bootstrap class
    private static $template = NULL;

    //Wire everything up
    public function Up()
    {
            //Get the bootstrap template
        self::GetTemplate();

            //Get each class
        foreach(self::$load as $class)
        {
                    //Load it...
            require_once "./class/" . $class . ".php";

                    //Build the static shell
            $bootstrap = str_replace("%CLASS%", $class, self::$template);
            eval($bootstrap);
        }
    }

    //Get the tempalte of the bootstrap class
    private function GetTemplate()
    {
        if(self::$template === NULL)
        {
                    //I've stored it in a separate file
            self::$template = file_get_contents("./class/Bootstrap.php");
        }
    }
}


Wire::Up();
//This is the Bootstrap class
//It offers a static shell for other instatiated classes
class %CLASS%
{
    public static $instance = NULL;
    public function __callStatic($method, $arguments)
    {
        if(self::$instance === NULL)
        {
            $class = get_called_class() . "Controller";
            self::$instance = new $class();
        }
        call_user_func_array(array(self::$instance, $method), $arguments);
    }
}
class Wire
{
    //List of classes I'll have to load
    private static $load = array("Foo", "Hello");

    //Template of the Bootstrap class
    private static $template = NULL;

    //Wire everything up
    public function Up()
    {
            //Get the bootstrap template
        self::GetTemplate();

            //Get each class
        foreach(self::$load as $class)
        {
                    //Load it...
            require_once "./class/" . $class . ".php";

                    //Build the static shell
            $bootstrap = str_replace("%CLASS%", $class, self::$template);
            eval($bootstrap);
        }
    }

    //Get the tempalte of the bootstrap class
    private function GetTemplate()
    {
        if(self::$template === NULL)
        {
                    //I've stored it in a separate file
            self::$template = file_get_contents("./class/Bootstrap.php");
        }
    }
}


Wire::Up();
//This is the Bootstrap class
//It offers a static shell for other instatiated classes
class %CLASS%
{
    public static $instance = NULL;
    public function __callStatic($method, $arguments)
    {
        if(self::$instance === NULL)
        {
            $class = get_called_class() . "Controller";
            self::$instance = new $class();
        }
        call_user_func_array(array(self::$instance, $method), $arguments);
    }
}

That's the Base class, for testing purpouses only

class Base
{
    public $value = 1;
}
Source Link

PHP my way of threating static classes

A few days ago I've faced an annoying problem. Let's imagine we have 3 classes: Base, System, Handler.

The classes Systen and Handler both inherit from the class Base. I want to access those classes this way: System::DoStuff(); I also need the Handler be available in the System, but do not want to instantiate it there, use any kind of reference injection.

I could simply design the System and the Handler class to be static. The problem would be: They need to inherit form the Base class, which should be static too, because if it's not, it won't work with the System and Hanlder because they ARE static. If is set Base to static, it won't work either, because once a child changes some parameter in it's parent, the parent would change for ALL children.

After some testing, I've came up with this solution:

class Base
{
    public $value = 1;
}

class Wire
{
    //List of classes I'll have to load
    private static $load = array("Foo", "Hello");

    //Template of the Bootstrap class
    private static $template = NULL;

    //Wire everything up
    public function Up()
    {
            //Get the bootstrap template
        self::GetTemplate();

            //Get each class
        foreach(self::$load as $class)
        {
                    //Load it...
            require_once "./class/" . $class . ".php";

                    //Build the static shell
            $bootstrap = str_replace("%CLASS%", $class, self::$template);
            eval($bootstrap);
        }
    }

    //Get the tempalte of the bootstrap class
    private function GetTemplate()
    {
        if(self::$template === NULL)
        {
                    //I've stored it in a separate file
            self::$template = file_get_contents("./class/Bootstrap.php");
        }
    }
}


Wire::Up();

I'me using the Wire class to load the classes I need. And use this Bootstrap file to create a static shell for each instatiated class.

//This is the Bootstrap class
//It offers a static shell for other instatiated classes
class %CLASS%
{
    public static $instance = NULL;
    public function __callStatic($method, $arguments)
    {
        if(self::$instance === NULL)
        {
            $class = get_called_class() . "Controller";
            self::$instance = new $class();
        }
        call_user_func_array(array(self::$instance, $method), $arguments);
    }
}

Here are the classes Foo and Hello that I've used above

class FooController extends Base
{
    public function Bar($args)
    {
        print $args . " " . $this->value . "<br>";
    }
}

class HelloController extends Base
{
    public function World($args)
    {
        $this->value = 10;
        print $args . " " . $this->value . "<br>";
    }
}

Now, if you call

Hello::World("Hello World") //Hello World 10
Foo::Bar("Foo Bar") //Foo Bar 1 
//If Foo and Base would be static, whitout the Bootstrap shell, you would see "Foo Bar 10"

What do you think about that solution? Am I hunting a mouse with a shotgun? Or maybe there are better solutions, for this scenario? I'd appreciate costructive critics!!