0

I'm working with php, codeignitor 3 and groceryCRUD. I'm trying to get access to variables inside of a callback function of groceryCRUD and tried to set it as a property to the this object. I wanted to reset or renew it on every foreach loop iteration and expected to be able to access it inside of the function like shown below. Unfortunately this doesn't work and I get an error 'Illegal Offset Type'. Thanks for any help :)

public function somefunc(){
    ...
    $array = [['one', 'two'], ['one', 'two']];
    foreach ($array as $x) {
        $this->my_prop_var = $x;
        $crud->callback_column('column_name',
            function ($fieldValue) {
                echo $this->my_prop_var[0];
                return something... ;
             }
        );
                
        unset($this->my_prop_var);
    };


};
kledo-34
  • 33
  • 5
  • Solved it, removing unset() makes everything work, I'm not sure why yet, because I'm not so comfortable with php yet, but feel free to explain if you understand things behind that ;) – kledo-34 Dec 12 '20 at 06:49
  • Does this answer your question? [Is it possible to delete an object's property in PHP?](https://stackoverflow.com/questions/3600750/is-it-possible-to-delete-an-objects-property-in-php) – El_Vanja Dec 12 '20 at 10:05
  • So, in short, unsetting doesn't just empty the value, it completely removes the property. And there is no need to do that, since your loop will overwrite the value anyway, you don't need to clear it "manually". Also note that you don't need semicolons after closing curly brackets (`};`). – El_Vanja Dec 12 '20 at 10:08
  • Thanks a lot, yes I got it, thank you for taking the time, understanding helps me a lot to ask fewer questions in the future ;) – kledo-34 Dec 14 '20 at 02:27

0 Answers0