I am carrying out a project in which I do not declare a constructor and, in this way, when creating an object, I only fill in the properties that I need at that moment. As an example:
class Publicacion{
private $id;
private $usuario;
private $fecha;
private $titulo;
private $contenido;
functions getters();
funtions setters();
function obtener_articulo(){
obtengo articulo a partir solo del id;
return resultado;}
}
In this case, if I only want to use the method, obtener_articulo()
I don't need to feed more than the property $id
:
$publi = new Publicacion;
$publi->set_id($el_id);
$articulo = $publi->obtener_articulo();
I feel it's very useful on a practical level but I wonder if it's a good practice or somehow it breaks OOP principles.
No OOP principle is broken, because even if you don't declare a constructor method it is called by default! . Still, it is normal to declare a constructor in each class.
By definition, the constructor method serves to inform the class, that is, to pass the data with which the object must be initialized.
In the case that you expose, if you want to keep to the usual standard, you could implement the concept of multiple constructor , thus, when creating the object you pass one, two, three... parameters depending on the case. Or you could make use of the optional arguments .
Reading your question, I think you're confusing things a bit:
You say that and show a code example where, using a setter , you inform the object. But it is that by doing that you do not fill the properties when creating the object, the object is first created empty when doing this:
$publi = new Publicacion();
, because the constructor is always called, even if you have not declared it in the class. What you do afterwards is to modify the previously created object.Just with the intention of shedding some light on your doubts, I want to list some disadvantages of doing what you want, without stopping at an exhaustive analysis of the situation:
The constructor is called anyway, even if you don't have one declared. Therefore, it is an illusion to think that you save something by not declaring it;
By not passing data through the constructor, to do it later through the setter , the object is created empty (not immediately usable);
After creating the object you modify it (applying a setter on it). This can have a negative impact on performance in contexts of concurrent use or on large objects.;
To inform the object with
N
fields you will have to modify itN
times by callingN
setters . This has an even more negative impact on performance. Thinking in a real context, it is like selling a vehicle without its basic attributes and then having the buyer return to the dealer to equip the vehicle with each attribute... for each attribute, a trip to the dealer, involving waste of time, money , fuel, pollution, etc, etc, etc.;If you have to validate certain fields... ufff you have to create verbose setters , when you can centralize all that in the constructor.
Some solutions
a) Multiple constructor
In other languages you would declare multiple constructors only creating the
N
constructors you need with their parameters, so depending on the number of parameters passed the corresponding constructor will be invoked. In PHP this is a bit more complicated, and a bit uglier, but PHP is like that :). But the ugly does not take away from the effective.This is an example of multiple constructors that makes use of
func_get_args()
and offunc_num_args()
, it is published in the contribution notes of the PHP Manual , with a wide acceptance by the programming community:Here the class
A
can be called in three different ways (I put the same examples from the PHP Manual):b) Using optional or default arguments
PHP allows the use of what is called Default Argument Values in the Manual . This allows you to declare arguments in the class's constructor that may or may not be passed.
If, for example, in your class the
$id
is always mandatory, the constructor would look like this:Thus declared the class, you can create instances of it with 1,2,3 or 4 parameters:
Instead of the optional arguments, you could also use an array of arguments and do the assignments based on that array.