PHP - Is It possible to return 2 new Class Object

Viewed 23

is there any possible to return a function in a multiple add new Class Object ? THanks in advance.

example :

public function model()
{
  return new Users(['...' => user1, .....]);
  return new Customers(['...' => customer1, .....]);
}
2 Answers

You can put them in an array and return both. You can then access the object you want and process it further.

Once the function reaches the return statement lines/codes below, that won't get executed. If you use an Advanced IDE (PhpStorm which I use) it will show you a message saying "Unreachable...".

error


To archive your goal, you can pass them in an array.

public function model()
{
    return [
        'user' => new User(....),
        'customer' => new Customers(...)
    ];
}
Related