2
I have the following class:
class Orcamento{
private $valor;
private $itens;
public function __construct($valor){
$this->valor = $valor;
$this->itens = [];
}
public function __get($property){
return $this->$property;
}
public function __set($property, $value){
if($property === "itens")
throw new \Exception("Utilize o método 'addItem' para alterar esta propriedade");
$this->$property = $value;
}
public function addItem(Item $item){
$this->itens[] = $item;
}
}
This class has two private properties, a getter and Setter constructor and methods. When I assign a value to a property that does not exist, immediately a new public property is created. As can be seen in the example below:
$orcamento = new Orcamento(600);
$orcamento->teste = "teste";
echo $orcamento->teste;
The above expression will print the string "test".
My question is:
- Is there any method that lists private properties?
- Is there any way to prevent the creation of methods and/or properties in a class?
- There are access modifiers for class and not only for methods and properties?
What would that be
get_class
within the method__set
? What is meant by "access modifiers for classes"?– Woss
It was a test for the get_class_vars() method. But it lists only public properties. When I talk about access modifiers for classes, I comment on the possibility or not of adding a modifier to the class creation so that it cannot be changed. I believe that this does not exist, this part would be just a doubt
– Mailson Teles Borges