Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:03:42+00:00 2026-06-12T13:03:42+00:00

I have been working on moving over to OOP in PHP. I have reading

  • 0

I have been working on moving over to OOP in PHP. I have reading explanations on php.net, but I was hoping I could get some specific answers here.

I tried to create the following example to illustrate my question. Say I have “Database”, “Products”, and “Users” classes, and I want to display products if a user has access.

So I call the “Products” class “showProducts()” function, which in turn creates an instance of the “User” class, which creates an instance of the “Database” object and checks the users access level.

If the user has access, then the “showProducts()” function creates another instance of the “Database” object, and queries the database.

class Database{

   public function query(){ 
      //runs query here 
   }

   public function __construct() { 
      //sets up connection here 
   }

}

class User{

   public function checkAccess(){ 
      $db = new Database(); 
      $db->query( //pass in query to check access )
      //does stuff, then returns true or false
   }

}

class Products{

   public function showProducts(){

      $user = new User();

      if($user->checkAccess())
         $db = new Database(); 
         $db->query( //pass in query to get products )
      }

}

I was hoping someone could illustrate how to do this the proper way.

I would like to have some sort of controller class, that creates one “Database” object, that is available to all of the classes that need to access it, without having to create multiple instances of the “Database” object. I would like the same thing with the users class, so there is one $users object that all the classes can access, without having to create a new object every time I need to use something in the “User” class.

I apologize if my question is not clear, and thanks in advance for any responses!!

Thanks to everybody for the replies!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:03:43+00:00Added an answer on June 12, 2026 at 1:03 pm

    When moving form procedural to Object Oriented programming you should grasp more then just how to build classes. OOP is not writing classes, its about following best practices, principles and patterns in OOP.

    You should not instantiate new objects inside another, you should give the User object, his Database object that User depends on, through constructor, or setter method. That is called Dependency Injection. The goal is to give objects to a class that needs them through constructor or setter method. And they should be instanciated from outside of that class, so its easier to configure class. And when building a class you want its easy to see what dependencies that class have. You can read about Inversion of Control principle here: IoC

    So then your code would look like this:

    <?php
    
    // User object that depends on Database object, and expects it in constructor.
    
    class User
    {
        protected $database;
    
        public function __construct($database)
        {
            $this->database = $database;
        }
    
        // -- SNIP --
    }
    
    ?>
    

    Now to use that user class you do this:

    <?php
    
        $database = new Database($connParams);
        $user = new User($database);
    
    ?>
    

    You can also use Dependency Injection using setter methods to set dependencies, but Il let you google that for yourself 🙂

    Thats it, joust read about Inversion of Controll principle, and about Dependency Injection and Dependency Injection Containers, these are the best ways to manage classes.

    I have seen lots of PHP code that is “OOP” and in fact they are only using Classes as functionality namespaces 🙂 So joust learn about OOP principles and patterns.

    Have fun! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been contemplating moving my project over to cloud 9 IDE but have been
I have been working on a project on and off, but I haven't touched
I have been working with the commercial version of Qt on Windows for some
I have been working with RabbitMQ on .Net for a while already and I
I've been working on this for hours, and reading over 20 articles and I
Working in .Net 2.0, moving the code base to 4.0 soon Recently I have
I have been working for some time on a library which performs numeric calculations.
I have been working with Netbeans quite a while and I am now moving
i have been working on moving one of my apps away from the shared
I've been working on moving our 9 projects in one SVN repo over to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.