Should I use setters with null or no setter when null value? 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
            -&gt;setFirstName($firstName)
            -&gt;setLastName($lastName)
            -&gt;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-&gt;setFirstName($firstName);
        }

        if (!null === $lastName) {
            $person-&gt;setLastName($lastName);
        }

        if (!null === $separator) {
            $person-&gt;setSeparator($separator);
        }

        return $person;
    }
}


Example call:

class PersonController extends Controller
{
    public function indexAction()
    {
        // get the PersonFactory instance
        $person = $this-&gt;get('person.factory')
            -&gt;createPerson('Bob', 'Whatever', '-');

        return $this-&gt;render('some/file.html.twig', [
            'person' =&gt; $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 ?
First off i'm a little confused by what  you are trying to do, and why you are doing this that way. I don't really grasp the full context of what and why you are doing it that way.


IMO since you are returning the same type of person, you don't need a person factory (update). You would need a person factory class if you were returning different types of People. Have a look at the method of polymorphism employed here: https://gist.github.com/BKSpurgeon/db4f8062569181d871f1565c89103e7e notice how we have different types of cities: LondonCity, SanFranCity - they all supply slightly different behaviour, but they are all cities.
setLastName not instance methods within the person class? Please have a look at the code below for what i mean. You probably should make these method private if you don't intend to call them.
"that's a no-no for OOP diehards"


this is what i mean.

# as OOP programmers we don't want to be doing things like this:

if x == 2 THEN do this
If x == 3 THEN do this
if x  == 4 THEN do this.
# As OOP programmers this is all we want to do:
#
cww = ClassWhichDoesWork.new(x)
cww.DoWork()
#
# and then we use a Factory method and polymorphism to supply the correct behaviour.
# we have no conditionals which say if x == 2 THEN do this. all of that is taken care of by polymorphism.
# if you do it that way you code will be very easy to maintain and to make changes, and to test as well.



Here is a gist which is probably how I would structure the above code given my very limited understanding of what you are trying to do