0

I have an existential doubt, I can not do that within a parameter that becomes a callback function enter and manipulate the same function, I have no idea what this would be like, but in this example it shows it.

<?php

class where{


    public function show_sql( $params ){

        if( is_numeric($params) ){
            echo "Number $params <br />";
        }elseif( is_callable($params) ) { // here validate if function
            // get_defined_vars() <--- I get the variables but I can not manipulate same function
        }
        return $this;
    }


}

$DB = new where;

$DB->show_sql(12)
    ->show_sql(13)
    ->show_sql(function ($object){
        $object->show_sql(14);
    });
?>

The result would be

Number 12 
Number 13 

But the number 14 is not shown and I call it in the same function. I want to have the same result as Laravel does in this example: https://laravel.com/docs/master/queries#where-clauses go to Parameter Grouping

Someone could help me ?

2
  • @MagnusEriksson ->show_sql(function ($object){ $object->show_sql(14); }); I think that here I am making the parameter a callback so that I can use the same function again but I do not know how to call the same function again Commented Jun 5, 2018 at 5:29
  • 1
    @Bdloul - is_callable() works for closures as well. It will return true as long as the variable can be called as a function. Commented Jun 5, 2018 at 5:33

1 Answer 1

1

You need to execute your callback.

You can use call_user_func_array() and pass the object as parameter:

}elseif( is_callable($params) ) {
    call_user_func_array($params, [$this]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks this worked !, how can I know when the callback starts and when it ends if I have several functions inside? example: $DB->show_sql(12) ->show_sql(13) ->show_sql(function ($object){ $object->show_sql(14); $object->show_sql(15); }) Could I use a Boolean variable to know when the callback starts and when the series ends? to get a result like that Number 12 Number 13 ( Number 14 Number 15 ) Number 16 where "()" are string.. How could I do it?
Just echo the parentheses before and after the execution. Something like echo ' ( '; call_user_func_array($params, [$this]); echo ' ) ';. If this answer solved the issue in the question, feel free to accept it.
it does not work, the result is: Number 12 Number 13 Number 14 Number 15 ()Number 16 I want the result to be Number 12 Number 13 ( Number 14 Number 15 ) Number 16 with function call like this $DB->show_sql(12) ->show_sql(13) ->show_sql(function ($object){ $object->show_sql(14); $object->show_sql(15); }) ->show_sql(16); should I use a public variable of the class where the string is concatenated? How could it be?
If you do as I suggested, it will work: 3v4l.org/EmL4V (if you check my previous comment, you see that I echoed one before the call and one after) - Also, before asking more questions, you might want to accept the answer I've already given you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.