I’m testing out somethings of Code Igniter and I noticed that code igniter doesn’t provide a way to let the user set parameters in the constructor of a model.
Then I searched around a bit and I found out that someone actually find it useless to have constructor in models. Why is that?
I’d love to do things like:
$user = new User(123); // 123 = id
$user->getName();
or something like that with models. But now it turns out that we shouldn’t use constructors for them.
Why should we or shouldn’t we use parameters for model classes?
I’m throwing that off my hat cause i don’t know anything about Code Ignitor but i know why most models usually feature a constructor less pattern.
The reason is for serialization et deserialization. Many languages (Vb.net and C# for instance) don’t allow serialization based on a constructor enabled class. Because, when deserializing a class that was serialized, it would have to go through the constructor which is impossible in deserialization process because it’s not part of the usual code path.
My guess is that the same thing occurs with Code Ignitor, they decided to remove the constructor for similar purposes even though there is the magic wakeup in PHP.
I think it makes sense, do you?