Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determinate when last method has been called in method chaining

I'm using something like this for a query builder:

$queryHandler = new QueryHandler($connection);
$queryHandler->insert("someDataHere")
             ->into("someTable")
             ->when("sleep", 1000);

I want it to execute the query when the last method has been called on that statement, which in this case is the when function.

/**
 * User: Bas
 * Date: 8-12-2014
 * Time: 20:11
 */
class QueryFunctions
{

    .....

    /**
     * @param int|float|string| $data The data which is getting inserted
     *
     * @return $this
     */
    public function insert($data) {
        ....
        return $this;
    }

    public function into($table, $execute) {
        ....
        return $this;
    }

    public function when($condition, $functionArgument) {
        switch($condition) {
            case "wait":
                .....
                break;
        }
        return $this;
    }

    /**
     * To prevent instantiation
     */
    private function __construct() {

    }
}

How can i do this?

like image 779
Bas Avatar asked Dec 03 '25 14:12

Bas


1 Answers

A common way is to invert the order of functions. Write:

$queryHandler->into("someTable")
             ->when("sleep", 1000)
             ->insert("someDataHere")
             ;

The insert() will trigger the query. I guess there is no other way in PHP except complex methods like workers or crontabs.

like image 70
Liglo App Avatar answered Dec 05 '25 03:12

Liglo App



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!