I would like to know what are the drawbacks of setting values this way:
class PersonFactory
{
public function createPerson($firstName = null, $lastName = null, $separator = null)
{
$person = new Person();
$person
->setFirstName($firstName)
->setLastName($lastName)
->setSeparator($separator);
return $person;
}
}
And if this way would be more clear, sustainable or efficient
class PersonFactory
{
public function createPerson($firstName = null, $lastName = null, $separator = null)
{
$person = new Person();
if (!null === $firstName) {
$person->setFirstName($firstName);
}
if (!null === $lastName) {
$person->setLastName($lastName);
}
if (!null === $separator) {
$person->setSeparator($separator);
}
return $person;
}
}
Example call:
class PersonController extends Controller
{
public function indexAction()
{
// get the PersonFactory instance
$person = $this->get('person.factory')
->createPerson('Bob', 'Whatever', '-');
return $this->render('some/file.html.twig', [
'person' => $person
]);
}
}
It feels like they are the same as null is already the value of an undefined variable, at least in PHP (looking at you Javascript).
So, would the only difference would be to call the setters even if it's not useful ?
PersonFactory
which creates Persons depending on the inputs of the methodcreatePerson
. I don't understand the expression: "that's a no-no for OOP die-hards" sorry. I don't understand either what you mean by "why are setFirstName/setLastName not instance methods within the person class" . Do you mean private/protected methods ? Otherwise, as these methods appears in the Person class and are called in this createPerson method, I don't understand why you say they aren't instance methods. \$\endgroup\$