is it acceptable to recycle or reuse variables?

Reusing variables for different purposes is a maintenance error waiting to happen.

It's bad practice. A well named variable can do wonders for aiding code comprehension.

Reusing variables is especially fragile in weakly typed dynamic languages such as PHP.

[In the past, I have been guilty of errors in code when reusing local loop variables like i and j ...]


The main reason to avoid reusing variables is that if you reuse a variable without properly re-initializing it, the old value will "leak" through, causing unpredictable effects and even security vulnerabilities. For example:

$foo = $_GET['input'];
# use $foo

if ($a == $b) {
    $foo = 1;
} else {
    # $foo = 2; # Commented out for some reason
}
# Value $foo supplied in URL leaks through to here

Generally, reuse of variables will not damage performance if the compiler uses single-static assignment form (SSA) as an intermediate form during optimization (part of what SSA does is giving separate names to such reused variables). So don't worry about performance - worry about maintainability.


What about when you need call another method of the DbObject ?

I prefer give the variable name what it is:

$dbo = new DbObject(new Database(), 'items');
$total_count = $dbo->count_all();

//so you call still do things next
$result = $dbo->get_all();