You can't, and you probably wouldn't want to even if you could.
You can't
There is no way to either determine if a method is going to be called later in the chain or force the caller to do so. In addition, PHP does not have any mechanism or constructs that allow you to execute code automatically at certain points (such as RAII in C++ or using
in C#).
You probably wouldn't want to
A big advantage of fluent interfaces is that they are composable:
$query = $db->doSth();
foo($query);
function foo($query)
{
$query->doSthElse()->query();
}
If you somehow forced the user to call query
at the end of the statement, the general-purpose function foo
could not exist.
See also
You might also find my answer to How to create a fluent query interface? useful -- there's nothing in there directly related to your question that I did not mention here, but "Step 3: Materializing" deals specifically with this part the interface.