0

I have a class within which I want to

  1. Set an array
  2. Loop through the array
  3. Invoke anonymous functions set in #1 as I loop through the array in #2

EDIT: The code below is working when tested out of my application, but in my CodeIgniter 3 Controller, I keep getting an error: Method name must be a string

My simplified code for the purposes of example (CodeIgniter 3):

<?php

class MyClass {

    // Setting my array
    public function my_arr ($options = array())
    {
        $arr = array(
            '1' => array(
                'a' => 'z',
                'b' => function($i) {
                    return 'c' . $i;
                },
            ),
            '2' => array(
                'a' => 'y',
                'b' => function($i) {
                    return 'd' . $i;
                },
            ),
        );

        return $arr;
        /**
         *
         * EDIT: Later in my code I found that there was some kind
         * of serialization/encoding attempt like:              
         * json_decode(json_encode($arr));
         */

    }

    // Doing My Loop
    public function do_loop()
    {
        $my_arr = $this->my_arr();
        $i = 0;
        foreach($my_arr as $key => $value){
            $anonymous_function = $value['b'];
            echo $anonymous_function($i) . '<br>'; // Keep getting `Method name must be a string`
            $i++;
        }
    }

}

(new MyClass())->do_loop();
Ryan Dorn
  • 679
  • 2
  • 8
  • 20
  • 2
    This code should never enter the loop at all, `my_arr()` doesn't return anything here. It also has invalid syntax. In this particular case you should provide with an example that is reproducible. Fixing those two issues I can't reproduce your problem at https://3v4l.org/2vLRs – apokryfos Sep 04 '22 at 22:03
  • Thanks @apokryfos . I fixed my mistakes / updated the code – Ryan Dorn Sep 04 '22 at 22:17
  • 1
    I still cannot reproduce your problem https://onlinephp.io/c/2e92c – apokryfos Sep 05 '22 at 05:48
  • Thanks again @apokryfos . After a bit more digging, I found that the issue had to do with serialization / encoding. I've updated my original question & title for clarity. – Ryan Dorn Sep 05 '22 at 10:18

1 Answers1

0

Posting back here in hopes that it helps.

My issue:

  • Later on in my codebase, I was trying to encode/serialize my method's output.

  • After a bit of reading, I found out that json_encode() & serialize() don't support serialization of closures / anonymous functions.

  • So, the Method name must be a string error happend because my attempts to encode/serialize - effectively - stripped the anonymous functions out & thus referenced a null function.

How I resolved the issue:

  • First I started with Opis Closure library to seralize the closure (I didn't end up going with this, but including it because I think it's a pretty cool library)

  • After the above, I just realized that I should refactor so that I wasn't trying serialize closures at all.

Ryan Dorn
  • 679
  • 2
  • 8
  • 20