Skip to content
Advertisement

How to call a local property using OOP?

I’m a beginner looking to learn more about PHP OOP, so there are things I don’t know, in the ShowUsers() method, I would like to display all users in the database, but I’m not getting it, because I don’t know how to call the connection property.

If I’ve been using encapsulation, it would be easy, but I’m using the connection property as local, and I really don’t know how to call this property, how can I call it without using encapsulation?

db.php

JavaScript

main.php

JavaScript

Note: I don’t want to use encapsulation to make it work, I want to learn how to call the variable without using encapsulation, if possible.

Advertisement

Answer

Based on the code above and the comments, I recommend that you declare $conn as protected in your DbConnect class:

JavaScript

Then in main.php, you can do:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement