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

  • SEARCH
  • Home
  • 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 7761555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:09:57+00:00 2026-06-01T14:09:57+00:00

Essentially, I am just creating two classes where one class, in this case class

  • 0

Essentially, I am just creating two classes where one class, in this case class A, runs a function in another class, in this case class B, to grab some information from the database.

However, when the B_runtime() actually calls upon the database I get the error Cannot access protected property A::$db.

What I don’t understand is that even though I have two __construct‘s in both classes the PDO statement is being very persistent on using the database connection from class A.

I am sure this has something to do with the fact that I am running the B_runtime() from within class A because this doesn’t happen if I call it from outside of class A.

I know that I can simply change the protected $db; in class A to a public variable, however, I am really curious as to how I can fix this.

ob_start();
include('/config.php');
ob_end_clean();

$A = new A($db);
$B = new B($db);

echo $A->A_runtime();

class A{
    protected $db;
    public function __construct($db){
       $this->db = $db;
    }
    public function A_runtime(){
        return B::B_runtime();      
    }
}

class B{
    protected $db;
    public function __construct($db){
       $this->db = $db;
    }
    public function B_runtime(){
        $preparedStatement = $this->db->prepare('SELECT * FROM z_mod_html WHERE ModuleLink = :moduleid LIMIT 1');
        $preparedStatement->execute(array(':moduleid' => '1'));
        $rows = $preparedStatement->fetchAll();
        return $rows[0]['HTML'];
    }
}

Sorry for the long amount of code – if anyone has any ideas or suggestions it would be greatly appreciated. Thanks.

  • 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-01T14:09:59+00:00Added an answer on June 1, 2026 at 2:09 pm

    You could pass the instance of B to the method. This way you also define the dependancy of your method on class B.

    $A = new A($db);
    $B = new B($db);
    
    echo $A->A_runtime($B);
    
    class A{
        //...
        public function A_runtime($instance){
            return $instance -> B_runtime();      
        }
    }
    

    You can even use type hinting in PHP 5 to signal that you are expecting an instance of class B there:

        public function A_runtime(B $instance){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class that essentially just holds some values. I have overridden
In our Pylons based web-app, we're creating a class that essentially provides some logging
I'm creating a base class Node that essentially wraps instances of another class from
I have a class which is essentially just holds a bunch of constant definitions
Essentially I have three fields and I am creating a new one which is
So essentially my question is this, I am creating an NSMutableDictionary using uint64_t objects
I am creating a webservice that essentially remotes some database calls (so that they
I'd like if someone could give me some advice on creating this script, which
I'm doing IPC between Java and some C code by just creating an external
Suppose my Facebook application is just one or two pages which makes use of

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.