开发者

PHP Define Variable containing a Class

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();
f开发者_Go百科unction 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();


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜