0

I'm attempting to define a __invokeable global instance of a class that contains my application's functions. Basically I'm trying to create a namespace for my library, and therefore I'm attempting to use a class to hold all my functions/methods.

I don't want to have to include global $class_instance at the top of all my files, because that is ugly.

Also I don't to have to reference the variable like $GLOBALS['myvar'] everywhere.

Personally I find this a real oversight in php.

It appears I can't define super globals like $myFunctionsGlobal And I can't define variables (well actually constants) in php like myvar=$classInstance.

Namespaces

If namespaces are supposed to solve this issue, why aren't they more widely used?

For example Kohana doesn't use namespaces, along with many other php libraries.

One I'm after:

class _namespace{
    public $_function;
    function __invoke($arg){
        // Function body
        echo $arg;
    }
    function method(){
        ;
    }
}
$N = new _namespace;


$N('someValue');
$N->method();
function myFunc(){
    // I don't want global $N;
    // I don't want $N = $_GLOBALS['N'];
    // I don't want $N = get_instance();
    $N('some other value');
}

Solution:

In most other languages like c and js you can only have one object/function per variable name. PHP seems to special allowing you to have namespaces,functions and classes with the same name. I was trying to group all of my functions under one central variable for simplicity and still have the functionality of it being __invokable. In fact a class and a function named the same thing would have provided this functionality.

<?

class R{
    static function static_method(){
        ;
    }
    function method(){
        ;
    }
}
function R(){;}

R();
R::static_method();

$instance = new R();
$instance->method();

In php5.3 you can emulate a invokable constant with methods by defining a function with the same name as your namespace.

namespace.php

<? namespace Z;

function init($arg=''){
    echo $arg;
}

function method(){
    echo 'method';
}

function method(){
    echo 'method2';
}

othefile.php

include('namespace.php');

function Z($a=null,$b=null){
    return Z\init($a,$b);
}

Z('test');
Z\method();
Z\method2();
5
  • What is your question? Is it just about namespaces? Commented Jun 21, 2011 at 22:17
  • Namespaces are not widely used in PHP because (a) they were only recently introduced, and (b) they're butt-ugly. (Anyway I'm not quite sure how they fit into the scenario you described.) Commented Jun 21, 2011 at 22:17
  • Can you show some code of the actual use you do, or is this a theoretical question before you start to code? Commented Jun 21, 2011 at 22:18
  • For what do you need to that __invoke? So you have a class that act's like one single function? PHP supports global functions (which could have a static vars to your object instance then), I really have problems to understand what you're aiming for. Please add some code. Commented Jun 21, 2011 at 22:41
  • I'm just trying to cut down on my global variables and provide the most versatility with my global variables. For example I could also add __set,__get,__call, etc. to the class above to add other potential functionalities. Commented Jun 21, 2011 at 22:49

2 Answers 2

1

Here's my new answer for you it works

class _bidon {
    static function __invoke($arg){
        // Function body
        echo $arg;
    }
}

$b = new _bidon;
$b('eee');

function myFunc(){
    // I don't want global $N;
    // I don't want $N = $_GLOBALS['N'];
    // I don't want $N = get_instance();
    _bidon::__invoke('some other value');
}
myFunc();

but the function will be specific to the class not the object

------ Previous post :

Hi i did not clearly understand but if you have a class created just do :

public static $myFunctionsGlobal;

and whene you want to use it outer than your class you do :

myclassname::$myFunctionsGlobal

and it will be accessible as soon as you include your class

you don't need to create an object because it's a static var you just need to have the class included

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a service container.

An example you can find here: Which pattern should I use for my unique instance of the User class? and to deepen If Singletons are bad then why is a Service Container good?

Also namespaces can't help you if you need to have one single instance for your helper objects like you are asking.

Addendum

With the service container I suggest you can still use __invoke.

$obj = app('CallableClass');
$obj(5);

14 Comments

I'm just trying to prevent global variable collision, while keeping my global variable __invokeable. By creating a class instance $classInstacce with methods I could successfully avoid global collisions. The class instance could also be invokable invokable.
@Lime: You should add the __invoke part to your question. I think that's important.
@hakre: he added that part after ... I hate when someoen does that
"added" at the top, yeah, easy to override then ... Not very constructive.
@lime: i have added infos about your invokable question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.