1

I am curious about the best practices and any performance or other considerations relating to passing an instance of an object as a parameter to another function in the same class vs creating another instance of that object in the new function. Here's a quick example:

Option 1: Pass both instance of Trainee AND TraineeController to other functions

protected function startTraining($traineeID) {
    $traineeController = new TraineeController();
    $trainee = $traineeController->findTrainee($traineeID);
    $this->initializeTraining($trainee, $traineeController);
    $this->doSomeOtherStuffWithTrainee($trainee, $traineeController);
    return Redirect::back()->with('trainee', $trainee);
}

protected function initializeTraining($trainee, $traineeController) {
    $trainee->blah1 = 'red';
    $trainee->blah2 = 'blue';
    $propertiesToUpdate = [
        'blah1' => $trainee->blah1,
        'blah2' => $trainee->blah2
    ];
    $traineeController->updateTrainee($trainee->traineeID, $propertiesToUpdate);
}

Option 2: Pass $trainee ONLY, instantiate a new TaineeController each time

protected function startTraining($traineeID) {
    $traineeController = new TraineeController();
    $trainee = $traineeController->findTrainee($traineeID);
    $this->initializeTraining($trainee);
    $this->doSomeOtherStuffWithTrainee($trainee);
    return Redirect::back()->with('trainee', $trainee);
}

protected function initializeTraining($trainee) {
    $trainee->blah1 = 'red';
    $trainee->blah2 = 'blue';
    $propertiesToUpdate = [
        'blah1' => $trainee->blah1,
        'blah2' => $trainee->blah2
    ];
    $traineeController = new TraineeController();
    $traineeController->updateTrainee($trainee->traineeID, $propertiesToUpdate);
}

In the above I need to pass $trainee across all functions each time instead of creating a new trainee from $traineeID because some other stuff goes on behind the scenes during the 'training' process that would otherwise be lost before relevant data is saved to the db. However, this is not required for TraineeController - I can either pass it as a parameter or instantiate a new TraineeController as much as I want. Which is the better choice?

I saw this question relating to C#, where the accepted answer was that passing an entire object is usually more efficient and instantiating another one because you are passing by reference. Does this hold true for PHP? Ie is the most efficient approach to pass the entire object by reference to required functions using &?

Community
  • 1
  • 1
spectralbat
  • 407
  • 2
  • 13

1 Answers1

1

There is nothing wrong with passing an object as reference, but note that php expects that your function argument needs to expect a reference rather than just passing a variable by reference (php docs). php 5.4.0 will even raise a fatal error if this is not respected:

right:

protected function initializeTraining($trainee, &$traineeController) {}
$this->initializeTraining($trainee, $traineeController);  

wrong:

protected function initializeTraining($trainee, $traineeController) {}
$this->initializeTraining($trainee, &$traineeController);  

Passing objects by reference will in most cases have better performance than initiating the object again, but passing by reference could become tricky if your object has its own properties:

class TraineeController {

  $fooCalled = false;

  function foo(){ $this->fooCalled = true; }

  function isFooCalled(){ return $this->fooCalled; }
}

$traineeController = new TraineeController();
$traineeController->foo();

//&$traineeController->isFooCalled() will be different from
//new TraineeController()->isFooCalled().
jan
  • 2,879
  • 2
  • 19
  • 28
  • Thank you, this helps. My controllers don't have their own persistent properties (they're basically just gateways to the other objects), so I think I'm going to try passing those by reference. Do you happen to have any input about passing by *value* vs creating a new instance in terms of performance, should such a case pop up? – spectralbat Oct 21 '14 at 10:24
  • I found some benchmarks here (not official): http://stackoverflow.com/a/6577819/982122 . Don't expect a giant increase in performance when passing your controllers by reference. It all depends on the situation. If you execute time-consuming code in the controller's construct, or looped for a significant number of times over an object instantiation, then using a reference could make a noticable difference. What i try to say is: pass by reference is not a magic solution for performance tuning. It's just handy in some cases. – jan Oct 21 '14 at 11:25