3

Is there a way to get the sibling functions of a function inside of an array?

I have an array of functions, similar to:

$thingy = [
  'do_something' => function() {
    // call $thingy['do_something_else']()?
  },

  'do_something_else' => function() {

    return 1234;
  }
];

Is there a way that I can call do_something_else from do_something? In other languages, like javascript, you should be able to use this or can use the variable name thingy.

  • 2
    Is there a reason this couldn't be an Object? – Richard Christensen Mar 17 '14 at 19:22
  • I don't think there's any way to do what you want implicitly, short of declaring the functions as methods of a class (*instead* of members of an array). The misleading thing is that in JavaScript, the functions have a `this` name that is implicitly the parent object, but doesn't *have* to be. You can reassign it using `.bind` or via various other means. In fact, it's the source of major confusion in JavaScript. – kojiro Mar 17 '14 at 19:27
  • @RichardChristensen Not really, but it is easier to dynamically add functions into it (not having to use `create_function`) and I was doing a few array functions on to it. –  Mar 17 '14 at 19:27

2 Answers2

6

Yes. You can achieve this by passing $thingy by reference:

$thingy = [
    'do_something' => function() use (&$thingy) {
        echo $thingy['do_something_else'](); // just an example
    },
    'do_something_else' => function() {
        return 1234;
    }
];

Example usage:

$thingy['do_something']();

Output:

1234

Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
1

Did a little digging and if you would rather use an object instead of an array. You can use this little php script found:

https://gist.github.com/Mihailoff/3700483

check out an question similar to yours:

Creating anonymous objects in php

it allows for you to create AnObj() which then allows you to keep an OOP approach when creating anonymous functions.

using the above linked php file you could do this.

$thingy = new AnObj(array(
    'do_something' => function(){
        echo 'do_something';
    },
    'do_something_else' => function( $thingy ){
        $thingy->do_something();
        echo 'do_something_else';
    }
));

$thingy->do_something_else( $thingy );

It's just my personal preference to use Objects whenever possible.

Community
  • 1
  • 1
Richard Christensen
  • 2,046
  • 17
  • 28
  • The OP is already using Closure objects, which is a pretty neat way as it is. I don't think there's a need for a separate class just for this. That's just my opinion though. – Amal Murali Mar 17 '14 at 19:47
  • Well as you can see you can pass `$thingy` to the `do_something_else()` function which allows for a more dynamic coding process which can allow for more `$thingies` which all have a slightly different `do_something()` function to be passed to the original `$thingy`. where as using an array you would always have to define $thingy every time something changed. Which for me allows for a more code reusability and loosely coupled design. Of course it really is a matter of preference. – Richard Christensen Mar 17 '14 at 19:51
  • Right. It depends on the use-case, though. +1 for an alternative approach! – Amal Murali Mar 17 '14 at 19:53