Object of class Closure could not be converted to string in: filename
You are seeing a Catchable fatal error because you attempted to echo a closure object directly, rather than invoking it. This is a common stumbling block when working with closures in PHP, especially when mixing static methods and instance properties inside callbacks.
In your specific example, the function convert returns a closure:
function convert($currencyType)
{
$that = $this;
return $result = function () use ($that)
{
if (!in_array($currencyType, $this->ratio)) // Note: $this here refers to the closure's context, which is null unless bound
return false;
return ($this->ratio[$currencyType] * $this->money);
};
}
The line echo $currency->convert('EURO'); executes the function and captures its return value. That return value is an instance of Closure. When PHP tries to cast that object to a string for output, it throws the fatal error you observed. Closures are first-class citizens in PHPâthey can be passed around like variablesâbut they must be explicitly executed with parentheses before their result can be displayed or used in calculations.
There is also a subtle but critical scoping issue in your code snippet regarding $this. Inside the closure, $this refers to the global context of the closure itself, not the original class instance stored in $that. However, since you are using use ($that), you likely intended to access the original object's properties. The current implementation defines a closure that references $this (which is undefined inside the closure unless bound) instead of using the captured $that variable.
To fix this, you need to invoke the closure immediately after creating it. Here is the corrected approach:
function convert($currencyType)
{
// Assuming 'convert' is a method within a class context where $this exists
$that = $this;
$result = function () use ($that, $currencyType) {
if (!in_array($currencyType, $that->ratio)) {
return false;
}
return ($that->ratio[$currencyType] * $that->money);
};
// Invoke the closure immediately
return $result();
}
$currency = new Currency();
// Execute convert and print the result directly
echo $currency->convert('EURO');
The key change is return $result();. The parentheses tell PHP to execute the function defined inside $result and return its output. Without them, you are just returning the closure object itself.
Additionally, ensure that the properties $ratio and $money exist on the Currency instance. If these properties are accessed via $that, make sure they are initialized in the constructor of your class.
Another pattern to consider is using arrow functions (PHP 7.4+) or closures bound directly to the instance method if you want to avoid capturing variables manually. For example, defining the logic as a standard method on the Currency class might be cleaner than returning a closure from another function:
class Currency {
public $ratio;
public $money;
public function convert($currencyType)
{
if (!in_array($currencyType, $this->ratio)) {
return false;
}
return ($this->ratio[$currencyType] * $this->money);
}
}
This approach eliminates the need for use clauses and makes the code easier to read. If you are working within a larger framework or need lazy evaluation, keeping the closure is valid, but remember that invoking it is mandatory before using its output.