I'm trying to upgrade my way of programming from Structured Programming (I don't know if it's really called that, but it's a bit messy) to Object Oriented Programming. I have been looking at object-oriented programming in the last month and I have the following doubt when I create an Object of type User
<?php
class usuario{
private $usuarioId;
private $usuario;
private $password;
private $rolId;
private $estado;
private $fc;
private $fm;
function __construct($usuarioId,$usuario,$password,$rolId,$estado,$fc,$fm){
$this->usuarioId = $usuarioId;
$this->usuario = $usuario;
$this->password = $password;
$this->rolId = $rolId;
$this->estado = $estado;
$this->fc = $fc;
$this->fm = $fm;
}
public function setPassword($password){
$this->password = $password;
}
public function getPassword(){
return $password;
}
}
?>
- Is it necessary to create a method
set
andget
for each attribute or is there some kind of shortcut (a way to create a unique method for it)?
You can use what are called the magic methods of PHP
Source Documentation