1
Example scenario
Root folder
- Classea.php
- Classeb.php
- index php.
Filing cabinet: classeA.php
class ClasseA
{
public $retorno = null;
public $error = "Erro desconhecido";
function __construct
{
$this -> func_A();
}
public function func_A()
{
require_once 'classeB.php';
$obj = new ClasseB;
$obj -> func_B();
}
}
Filing cabinet: classeB.php
class ClasseB extends ClasseA
{
public function func_B()
{
$this -> error = "Erro em func_B";
}
}
Filing cabinet: index.php
require_once 'ClasseA.php';
$obj = new ClasseA;
echo ($obj -> retorno != null) ? $obj -> retorno : $obj -> error;
Problem
My return from index.php
is: "Unknown error".
What did you expect: "Error in func_B".
Doubt
- Why property
error
ofClasseA
is not amended? - What possible solutions?
I think you have your answer https://answall.com/questions/247723/diff%C3%A7as-quanto-ao-uso-de-this-self-static-e-parent
– Alvaro Alves
see Anderson’s response Carlos Woss
– Alvaro Alves
Your problem is that when instantiating an object
ClasseB
assign its error value, you are changing only this instance and not the class instanceClasseA
. Perhaps callingparent::error = "Erro em func_B"
infunc_B
work, but I find it difficult since each instance is independent of the other– edson alves
@Alvaroalves, I could not extract my answer from this link, could say the excerpt please?
– rbz
@edsonalves Call for
parent::
it would have to be static, and if it’s static it wouldn’t change it.– rbz
It is not the answer itself, but how you are treating this problem, as Maniero’s answer says: "This scenario is atypical, and I doubt it has any real use, but if you are wanting to do something real, it seems to be wrong engineering and the path must be quite different from this tried."
– Alvaro Alves
@Alvaroalves It may be that yes, it may be that no, it may be maybe, etc... kkk
– rbz
@Sometimes I have some crazy ideas around here too, we are creators of things, it is normal sometimes to create something that is not understood by other kkk
– Alvaro Alves
@Alvaroalves Complicated! rs... It is very particular how to "write a code", and sometimes it could use otherwise, but if it helps me to organize, understand, work, etc., or it would have to be a very good argument/example to get the idea (which is difficult because there is no whole context)or reach a point that has no option, and yes, it needs to change! rs
– rbz