Use/change property of an extended class and print by instance

Asked

Viewed 104 times

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 of ClasseA is not amended?
  • What possible solutions?
  • 1

    I think you have your answer https://answall.com/questions/247723/diff%C3%A7as-quanto-ao-uso-de-this-self-static-e-parent

  • see Anderson’s response Carlos Woss

  • Your problem is that when instantiating an object ClasseB assign its error value, you are changing only this instance and not the class instance ClasseA . Perhaps calling parent::error = "Erro em func_B" in func_B work, but I find it difficult since each instance is independent of the other

  • @Alvaroalves, I could not extract my answer from this link, could say the excerpt please?

  • @edsonalves Call for parent:: it would have to be static, and if it’s static it wouldn’t change it.

  • 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."

  • 1

    @Alvaroalves It may be that yes, it may be that no, it may be maybe, etc... kkk

  • @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

  • 1

    @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

Show 4 more comments

2 answers

4


I admit that I may be mistaken, because PHP tends to have strange behaviors, I will answer for the universal logic that I know, even because I can’t reproduce this because I only have PHP in these Ides online who enter into loop with a building like this the way you need to put in them.

You are creating a totally different object within func_A(), even if it is derived from ClasseA, this object stored in $obj inside func_A() is not the same object that is operating on ClasseA. When you use the $this in the object of ClasseB, that is, of the $obj, is not changing the $this of $obj global, are very different objects, and what you’re having printed is this object only.

Recalling that the $obj class internal exists only as long as an object of that class exists, but it is completely separate, only possesses a bond of association, is a composition (a Groso mode an object within the other), is not inheritance (a single object with two models).

The fact that two completely different variables have the name may have made understanding difficult.

This scenario is atypical, and I doubt it has any real use, but if you’re looking to do something real, it seems to be wrong engineering and the path must be very different from this one.

  • But if I’m declaring $this -> error = "Erro em func_B"; and does not "have access" to property error of ClasseA, and/or there is also no such property in ClasseB, should accuse an error (PHP failure!?). So there is no way to change this property in this structure?

  • I didn’t say you don’t have access ClasseA, I said that you do not have access to another completely different object, the problem is not having two classes is having two objects. The $error exists in ClasseB because this class derives from CLasseA which has the field (not a property) that you want, but is of another object, another instance. Both have cigarette lighter, but one is your car and the other is my car. If you start yours will not heat mine. has how to change, but depending on the case, this scenario serves for nothing, in real scenario I would think how to do.

  • But the object of A is in the index. The function of A creates one of B to change its property. So it doesn’t exist? Doesn’t it work? If there were no object of B then would it be possible to change ? I did not understand if there is a way...

  • Well, you’re already using inappropriate terms, and I don’t know what you’re talking about. Everything would work if I was done the right way, and nothing will work if you do it the wrong way, no matter if you have an object of ClasseB or not. I can’t say what’s right because I don’t even know what it’s for, as far as I can see, serves for nothing, but it’s just because it’s confusing and doesn’t indicate the goal.

  • But if you were to think so, then everything would fall into the "XY problem", and nothing would answer the question. The problem is in the example scenario, is to change the property of the extended class. I do not understand the upvotes! rs

  • I can’t tell you everything, but this seems to be an XY problem, you asked for an explanation of the mechanism, I answered. Now you want the solution to a problem, it’s another completely different issue. But to ask about the solution needs to be very clear about the problem. At this point it does not seem to know what the problem is. When you don’t know where you want to go, any path will do because everyone will take you to the wrong place. Artificial scenarios only works for the mechanism, in real problem depends on the context and complete, can not miss any details, have q know what the problem is precisely

  • The logic is Classea, being a "master" class, it receives a call and processes according to the directory / file / class / method dynamically. Then it will always call a class/method being this method, the last processing, and if it has error, it will set the "error" property of Classea, if it has no errors, it will set the "return" property of Classea, for example. Apparently, I will have to return an array or json, and process it in Classea, correct!?

  • I do not know, for me all this is wrong and the solution is different, but again, just seeing all the details, really, trying to program over artificial things does not work, there is no semantics, so everything can be right or wrong.

  • I will create a more specific question, so if it does not give, it is practically "throw everything away", then my question will be "show me how to do" because they would close the options, if before is not closed by "too wide" or "based on opinions"! rs

Show 4 more comments

1

It’s not exactly what you were trying, but maybe this can help you:

class ClasseA
{
    public $retorno = null;
    protected $error = "Erro desconhecido";

    public function __get($name)
    {
        require_once 'ClasseB.php';
        $obj = new ClasseB;
        $obj->func_B();

        return $obj->$name;
    }
}

Besides, I’m not sure what would be the use of calling the classeA or instead of instantiating the classeB directly (since it extends A). The error in your code (as best explained by Maniero), is that the variable $obj exists only within the context of the called function and does not override the attributes of the class.

Now using the Magic Function __get, you can override the call of the non-public methods.

  • This would not solve me, because it would not only be 1 property, it would be several! But I appreciate the answer. :)

  • Yes, in case the __get method intercepts any non-public property (protected), and then you can insert your validation logic for certain purpose.

  • Another solution is to create a Factory, ai instead of instantiating the object directly, you call the Factory which returns the correct instance of the desired object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.