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();
Lime
  • 13,400
  • 11
  • 56
  • 88
  • What is your question? Is it just about namespaces? – Lightness Races in Orbit Jun 21 '11 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.) – Lightness Races in Orbit Jun 21 '11 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? – hakre Jun 21 '11 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. – hakre Jun 21 '11 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. – Lime Jun 21 '11 at 22:49

2 Answers2

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

darkylmnx
  • 1,891
  • 4
  • 22
  • 36
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);
Community
  • 1
  • 1
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • 1
    I'm just trying to prevent global variable collision, while keeping my global variable `__invoke`able. By creating a class instance `$classInstacce` with methods I could successfully avoid global collisions. The class instance could also be invokable `invokable`. – Lime Jun 21 '11 at 22:18
  • @Lime: You should add the __invoke part to your question. I think that's important. – hakre Jun 21 '11 at 22:35
  • 1
    @hakre: he added that part after ... I hate when someoen does that – dynamic Jun 21 '11 at 22:36
  • "added" at the top, yeah, easy to override then ... Not very constructive. – hakre Jun 21 '11 at 22:37
  • @lime: i have added infos about your invokable question – dynamic Jun 21 '11 at 22:39
  • @yes123 yeah but then I have to call my `app` function before I use the variable in every function or method. – Lime Jun 21 '11 at 22:42
  • @Lime: if you don't want that the only thing you can do is to use `$_GLOBALS[]`. There aren't any other options – dynamic Jun 21 '11 at 22:43
  • Or even better, store the name of the app function inside a global and then do a variable function call to have a "variable". LOL -> `$_GLOBALS['app']='app'; $$_GLOBALS['app']('CallableClass')(5); # PHP 6 maybe` – hakre Jun 21 '11 at 22:44
  • lol. Not to mention there are just better ways to do that instead to use that magic methods __invoke that I think almost no one uses – dynamic Jun 21 '11 at 22:45
  • For PHP 5 compatibility an array access could be mapped into calling the provider returning it's result by index. Then __invoke can actually work on the return value even with the PHP 5 syntax parser. Yeah. – hakre Jun 21 '11 at 22:48
  • @Lime: your solution now doesn't use at all class neither __invoke.. I guess you totally changed your mind – dynamic Jun 21 '11 at 23:37
  • Its kinda hard to describe something you don't know the name of. I was looking for the functionality. – Lime Jun 21 '11 at 23:48
  • @es123: Looks like someone found out about Namespaces. – hakre Jun 21 '11 at 23:49
  • I had known about `namespaces`, but I hadn't realized they didn't pollute the same namespace as functions and classes. 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 the end I just define a `class` and `function'. No need for any `__invoke` trickery. – Lime Jun 22 '11 at 00:25