-2

Possible Duplicate:
Best practices for static constructors

I want to create an object similar to the way jQuery is set up, that means have a main function and utility functions. I was thinking of doing something like this:

class main {
    function __construct() {
        echo 'this is the main usage of the object';
    }
    static function helper() {
        echo 'this is a helper function';
    }
}

So using the helper is pretty straightforward: main::helper(); but in order to use the main function itself I need to use the new keyword everytime: new main(). Is there any way to make it so that I can call the main function without using the new keyword?

Community
  • 1
  • 1
qwertymk
  • 34,200
  • 28
  • 121
  • 184
  • You need to concretise your usage intentions. It's not easy to make the connection between jQuery-like and your intended static function calls (which you already know how to do). – mario Oct 14 '12 at 01:46

1 Answers1

0

Call with helper for example nameofhelper:

main::helper('nameofhelper');

Function class:

abstract class main {
    function __construct() {
        echo 'this is the main usage of the object';
    }

    public static function helper($name_of_helper)
    {
        $helper = new 'Helper_'.$name_of_helper;
    }
}

Helper class:

class Helper_nameofhelper
{
    function invoke()
    {
        return 'invokedhelper';
    }
}
Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53