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 8094287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:45:14+00:00 2026-06-05T20:45:14+00:00

I hava a class called DBhelper, and now I created a new class called

  • 0

I hava a class called DBhelper, and now I created a new class called UserManager.
I need the UserManager have functions like GetUserId() and CheckValidation().So I require the DBhelper for those database actions. But I found it doesn’t work like this:

require_once('DBhelper.php');
class User{
  public $databaseHelper = new DBhelper();//syntax error
  public $DB = $databaseHelper->connectDB();//syntax error

  public function GetUserId(){
    $this->databasesHelper->RunSql();//syntax error
    ...
  }
  public function CheckValidation(){
    $this->databasesHelper->RunSql();//syntax error
    ...
  }
}

Please halp me, I have searched the internet for long time. 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-05T20:45:16+00:00Added an answer on June 5, 2026 at 8:45 pm

    Try something like this, it uses the dependency injection design pattern.

    class User {
        private $db;
        public function __construct(DBHelper $db) {
             $this->db = $db->connectDB();
        }
    
        public function GetUserId(){
            $this->db->RunSql();
        }
    
    }
    

    Here is how you instantiate it:

    // Assuming: require( 'DBhelper.php');
    $dbh = new DBHelper();
    $user = new User( $dbh);
    

    Also, to touch on your errors, these are errors (note they’re not syntax errors, since they are syntactically correct):

    public $databaseHelper = new DBhelper();//syntax error
    public $DB = $databaseHelper->connectDB();//syntax error
    

    Because you cannot initialize member variables to non-static values. So, since the values are not known at compile time, it is an error.

    Similarly, these calls:

    $this->databasesHelper->RunSql();//syntax error
    

    Are invalid because $databasesHelper was never instantiated with a proper object.

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

Sidebar

Related Questions

I hava a generic class called ArrayBag. I want to override equals method. So
I hava a supeclass called Car with 3 subclasses. class Ford extends Car{ }
I have a Player class that extends CCNode, and i want to hava a
I hava java swing project which is developed in JBuilder IDE. Now I need
I hava a url like mysite.net/home/index/page/XX while XX is any number. I need to
i have some small problem. I hava some class Processor and Process . Process
I hava some code like this: class LooperThread extends Thread { public Handler mHandler;
I hava a jar with a main file like this: public class StartMesekocka3D {
I hava a Java mulithreading question. I have the following worker class: public class
I hava an activity which might have to load data when it's starting up.

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.