Home > Net >  PHP - Is It possible to return 2 new Class Object
PHP - Is It possible to return 2 new Class Object

Time:09-14

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, .....]);
}

CodePudding user response:

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

CodePudding user response:

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