Welcome to Stackoverflow.
Classes usually have a setter and a getter for each attribute or property of them.
This means that, in this case, the class Automovil
should have a setter for the properties $idautomovil, $marca, $modelo, $anio
... and also a getter .
The setter would serve to change a given property once the object has been created. When the attribute of a class is private
you can not modify it from outside the class by means of the assignment $instancia->atributo="valor"
, as in fact you try to do. This would violate one of the fundamental principles of OOP, which is known as encapsulation .
The getter would serve to get that property.
Another aspect is the constructor of the class. Within it you can decide two things:
-
If the class will be created always empty of properties which will be set a posteriori (using the setter ). In that case the class will have an empty constructor:
function __construct() {
}
And the instance creation of the class would be written like this:
$myAuto=new Automovil();
-
Or if it will be created with all or some of its properties. In this case, the constructor should receive the values of the properties in parameter and set them with $this
:
function __construct($idautomovil, $marca, modelo,$anio,$tipo) {
$this->idautomovil =$idautomovil;
$this->marca=$marca;
$this->modelo =$modelo;
$this->anio=$anio;
$this->tipo=$tipo;
}
And the instance creation of the class would be written like this:
$myAuto=new Automovil($idAutomovil,$marca,$modelo,$anio,$tipo);
An example of your class could be this, understanding what is explained above:
class Automovil {
private $id;
private $marca;
private $modelo;
private $anio;
private $tipo;
function __construct() {
}
public function setMarca ($marca) {
$this->marca=$marca;
}
public function getMarca () {
return $this->marca;
}
}
We tried to create an instance:
$myAuto=new Automovil();
/*Seteamos la marca*/
$myAuto->setMarca("Nissan");
var_dump($myAuto);
Exit:
object(Automovil)#1 (5) {
["id":"Automovil":private]=>
NULL
["marca":"Automovil":private]=>
string(6) "Nissan"
["modelo":"Automovil":private]=>
NULL
["anio":"Automovil":private]=>
NULL
["tipo":"Automovil":private]=>
NULL
}
As you can see, the object has its mark.
I have only put a getter and setter for brevity reasons, you must complete the class with the other getter and setter for each property of the same.
Use of magical methods __set
and __get
If you want to use the magic methods, for the same thing that we have said above, it is necessary to know that the method __set
would be to assign the property (not to return anything), that function would correspond to the method __get
.
These methods would then be written like this:
public function __set($k, $v) {
$this->$k = $v;
}
public function __get($k) {
return $this->$k;
}
The problem is that in your method __set
you were not assigning the value to the property of the class.
Is it a good practice what you are doing?
In short, it is a terrible practice. Leaving an open __set
method would violate with astonishing ease one of the basic principles of OOP: encapsulation.
That means that someone could assign a strange property to the object Automovil
, for example:
$myAuto->fakeProperty='Esta propiedad no existía y se creará arbitrariamente';
If you review the object with var_dump
you can see that in effect, that property has been created in your object:
...
["fakeProperty"]=>
string(55) "Esta propiedad no existía y se creará arbitrariamente"
...
Which is to say that it is not encapsulated.
Actually, the true utility of __set
is precisely ** defend the encapsulation of the object , throwing an exception and forcing to go through the setter legitimate to modify the object:
public function __set($k, $v) {
throw new Exception ('No se puede violar la encapsulación');
}
Doing this, when you try to modify the object by adding a fakeProperty
, an exception will be raised:
PHP Fatal error: Uncaught Exception: No se puede violar la encapsulación in source_file.php:25
Stack trace:
#0 source_file.php(35): Automovil->__set('fakeProperty', 'Esta propiedad ...')
#1 {main}
thrown in source_file.php on line 25
For more details, I would recommend that you read the contributory user notes in the PHP Manual.