Using Methodo within another Methodo with extends class

Asked

Viewed 33 times

-1

Hello, I’m learning PHP and I’m having a class inheritance problem.

I have a class for connection and another for crud but I can’t use the connection in the crud class.

someone can help me with the mistake?

Grateful!

inserir a descrição da imagem aqui

  • That’s good, isn’t it? Because inheritance must be used for things that have direct relation, that is, the daughter class, has to be exactly the same as the mother class with something more or different, inheritance does not exist to join two things that have nothing to do with each other.

  • Good night, Reinaldo. You forgot the $this-> when calling mother class method.

  • I don’t understand "obsession" with various questions and programmers, with using OOP for relatively simple things because someone said it would be "better". I think that anything "more advanced" should be used according to real need and not because everyone uses it or because they think it will look better/easier. I even understand the "abstraction" that people seek so much, but the problem is that simply the use of OOP ends up turning into a salad instead of solving problems, because it seems to me that many people have the wrong view of Oop. It’s just a question of mine, don’t get me wrong. (cc @Maniero)

1 answer

1


Your problem is quite simple to solve. There is a pseudo-variable in PHP called $this that represents the context of the object, with it you can access what your object represents, namely: methods, variables and even inherited methods and variables.

In your case, the problem can be solved by changing getConnection() that is within the method getPlanejadores() for $this->getConnection(). If you look at what has happened with this change, we are talking about that we want to access within our object (using $this) the method getConnection(). It was clear?

That way your problem is solved, but I’d like to warn you of something. As you said, you are still learning PHP and that’s fine, but in this example you used inheritance in the wrong way. You use inheritance when you want to create a sub-type of something, in which case you would use inheritance when you want to create a sub-type of Connection but Planejador is not a sub-type of Connection, it just uses. I advise also on this journey of his studies, to delve a little into object orientation.

Browser other questions tagged

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