jeudi 18 mai 2017

passing variables vs instances to constructor

I have a class named Locations, it should accept 3 parameters name, city_id, country_id.

What is the best solution for passing these values, should i pass variables or instances of their classes.

$location = new Locations('Dubai, UAE', 1, 2);
$location = new Locations('Dubai, UAE', $cityId, $countryId);

I believe for the sake clarity and the single responsibility i should use dependency injection

$location = new Locations('Dubai, UAE', new City('Dubai'), new Country('UAE'));

When and why should i use variables (IDs) instead of instances?

Aucun commentaire:

Enregistrer un commentaire