1
I am venturing into the world of POO in PHP and I came up with the following question.
Of the four ways I used in the code below, to assign the value of $prop1
of MinhaClasse
to the variable $val
within the method fazAlgumaCoisa()
of OutraClasse
, which would be the most appropriate in terms of performance and safety?
Note: please take into consideration that in addition to $prop1
, I will have many other similar properties in the same code.
<?php
class MinhaClasse {
static $prop1 = 'Valor 1';
public static function set($name,$val)
{
self::$$name = $val;
}
public static function get($name)
{
return self::$$name;
}
}
class OutraClasse {
public $propA;
public $propB;
public function __construct()
{
$this->setProps();
}
public function setProps()
{
$this->$propA = MinhaClasse::$prop1;
$this->$propB = MinhaClasse::get('prop1');
}
public function fazAlgumaCoisa()
{
// Abaixo Diferentes forma de obter "Valor 1" da "MinhaClasse".
$val = MinhaClasse::$prop1; // 'Valor 1'
$val = MinhaClasse::get('prop1'); // 'Valor 1'
$val = $this->$propA; // 'Valor 1'
$val = $this->$propB; // 'Valor 1'
// o método fará algo à mais a partir daqui...
}
}
I get it. I’m actually using this code more to learn POO (OOP) anyway, so I wondered what would be the most indicated way to get 'Value 1' within the context I mentioned, following the best practices, conventions, etc... Anyway thank you for the answer. I will analyze the links you mentioned. Obg
– robssanches
@robssanches If I told you that "POO is not code", what would you reply?
– Woss
@Anderson Carlos Woss Now I would say that I believe you are right and that POO would not be the code itself, but rather the methodology or concept used behind the writing, to allow the organization and possibility of reusing the code. But I don’t know if I’m right in that definition...
– robssanches
@robssanches what concepts? And how your code relates to them?
– Woss