Gomi
Gomi

Reputation: 662

PHP Change of object class

i use classes for some typical MySQL queries, like getting specific users, news etc. And afterwards I call function query(), which calls mysqli class and query and returns the query like this:

class Users {
  [...]
  public function query() {
    $mysql = new mysqli([...]);
    $mysql->query($this->query);
    return $mysql;
  }
}

When I use it for ex. in $variable like:

$variable = new Users();
$variable->setNew($params)->query();

I have still stored object of Users in $variable. Is there any way to automatically 'update' the variable to be object of mysqli_result (in this case or different class in simillar situation) without using $variable = $variable->setNew($params)->query();?

Thank you for your advice.

Upvotes: 1

Views: 183

Answers (1)

Daan
Daan

Reputation: 12236

You can do this in a one liner:

$variable = (new Users())->setNew($params)->query();

$variable now contains the return of query()

Upvotes: 2

Related Questions