Catch instance of daughter class

Asked

Viewed 849 times

1

I have a class:

class Children extends Database

Soon, Children is the daughter class and Database the parent class, in the parent class I have the attribute:

protected $object = null;

The value of it must be the instance of the child class, currently to set this attribute I am using:

// Construtor da classe Children
public function __construct($id = null, $daddy  = null, $people = null)
{
    $this->id = $id;
    $this->daddy = $daddy ;
    $this->people = $people;
    $this->object = $this;
}

But straight I forget to put the line $this->object = $this; in the manufacturer, and this affects the functioning.

If I put $this->object = $this; in the Database class constructor will not work because it will store the instance Database attribute, not the class instance Children.

There’s a way in the father class builder I set that attribute worth your son?

For when I give one new Children() the attribute object class Database Already worth that instance, taking away the need to have the $this->object = $this; in the builder of the daughter class (Children).

In doing new Children() the goal would be:

// Construtor da classe Database
public function __construct()
{
    $this->object = instancia criada de Children
}
  • I didn’t understand the last paragraph, when creating a Children you want that somehow it changes the state of Database?

  • That’s more or less what it would be like to create a Children want to store instance of it in class attribute Database. The difficulty is to indicate the child class in the Database. The attribute object should store the child class instance, which in this case is Children. In short, it would be to do the same as $this->object = $this, only in the parent class (Database)

2 answers

6


There is no way, the daughter should always refer to the mother and not the other way around. You have to do it in hand. What you could do is the parent constructor accept a parameter that takes the instance and stores in the variable, but the parent constructor call will be required.

I keep thinking, what if the DataBase don’t have a daughter?

In fact it makes little or no sense. Nor will I get into the merit of a class that is not to be a Database If you’re inheriting that, it could just be a bad example. But an object having a property whose value is the very instance that loads it is at least odd. I can’t talk much because I don’t know the general context, but this feels wrong.

  • It is impossible for her not to have a child, if not, she will only be useless. I will briefly quote the use, the Children is a normal model, with its attributes, such as name, plus two mandatory attributes (primaryKey and table). The class Database is responsible for storing methods such as insert and listAll. The attribute object that saves the instance of the daughter will only take the attributes as name, at the time of storing in the bank. Doing this makes it possible to just use $children->insert() to store that object on the bench, without the need to Database::insert($children);

  • Is it abstract? How so mandatory attributes? If they are mandatory they must be in the mother class. All attributes that the class must access must be known to you, must be declared in it. This model seems to be quite wrong.

  • Yes it is abstract, really should be in the mother class, I was thinking that users would be nice kk, I will move them to the mother, thanks for the warning.. I don’t see how wrong, the class Database "scan" which was passed to it as an object seeking and knowing its attributes. Of course, in other languages it would not work.

  • If you want to change, then you were wrong. But I still don’t see the relationship you’re seeing between these things. Maybe if I saw it working I could see, but it doesn’t look like it should be like this.

-1

It is possible to obtain the name of the daughter class in the parent class as follows:

$this->classObjectChild = get_called_class();

  • If you read the documentation of this method, you will notice that this does not solve the problem. This function returns the class name "Called by the static method" and it doesn’t even fit the above problem.

Browser other questions tagged

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