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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:17:03+00:00 2026-05-31T17:17:03+00:00

I have config.php with this… $dbhost = localhost; I want to be able to

  • 0

I have config.php with this…

$dbhost = "localhost";

I want to be able to use the $dbhost variable inside a class.

User.class.php

include 'config.php';    
class User {
    private $dbhost = ???
    }

There’s a couple other questions like this, but they were for some other specific uses, and I guess it’s just a basic thing that I can’t really find anything else about it on the web.

UPDATE: Wow, thanks for all the help everybody. New to this site, but I might just stick around and give back where I can.

  • 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-05-31T17:17:04+00:00Added an answer on May 31, 2026 at 5:17 pm

    several ways I think.
    First pass it to the class constructor:

    include 'config.php';    
    class User {         
        private $dbhost;
    
        function __construct($dbhost){
          $this->dbhost=$dbhost;
        }
    }
    $user= new User($dbhost);
    

    Or use a setter:

    include 'config.php';    
    class User {         
        private $dbhost;
    
        function setDbhost($dbhost){
          $this->dbhost=$dbhost;
        }
    }
    $user= new User();
    $user->setDbhost($dbhost);
    

    Or using CONSTANTS:

    define('DBHOST', 'localhost');
    class User {         
        private $dbhost;
    
        function __construct(){
          $this->dbhost=DBHOST;
        }
    }
    

    OR using global:

    include 'config.php';    
    class User {         
        private $dbhost;
    
        public function __construct() {
        global $dbhost;
        $this->dbhost=$dbhost;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well i have this set of codes <?php include includes/config.php; class template{ var $page;
Hallo I have this script: <? require(lib2/config.inc.php); require(lib2/tpl.class.php); require(lib2/db.class.php); require(lib2/um.class.php); $tpl = new template(templates,
I have many PHP files in / /client/ /user/ /config/ etc... I want all
This is my registration.php include(config.php); if(isset($_POST['submit'],$_POST['fullname'],$_POST['username'], $_POST['password'], $_POST['email'], $_POST['role'])) { $submit=$_POST['submit']; $date=date(y-m-d); $fullname =
I have a file called config.php, I want it to remain exactly as is,
I have these three files to work with: document_root/include/config.php document_root/include/database.php document_root/index.php And the relevant
I have this setting. root dir| index.php config.php file.php | | |scripts|a.js | |account
I have the following in my config/routes.php file: $route['(:any)'] = dashboard/index/$1; This works as
This is about RAW php coding. I have a page - config.php and there
As many do I have a config.php file in the root of a web

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.