3

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.

4 Answers 4

4

You could use a global variable, defining a constant would be better, but using a setter/getter method is best. When you are using a class, any outside resource that it uses should be passed to it. This design patter is called dependency injection if you want to research it further.

In this example, I've combined the setter and getter into a single method, and included the ability to set the value when you first create the instance, using a constructor:

class User
{
    private $host = null;

    public function host($newHost = null)
    {
        if($newHost === null)
        {
            return $this->host;
        }
        else
        {
            $this->host = $newHost;
        }
    }

    function __construct($newHost = null)
    {
        if($newHost !== null)
        {
            $this->host($newHost);
        }
    }
}

//Create an instance of User with no default host.
$user = new User();

//This will echo a blank line because the host was never set and is null.
echo '$user->host: "'.$user->host()."\"<br>\n";

//Set the host to "localhost".
$user->host('localhost');

//This will echo the current value of "localhost".
echo '$user->host: "'.$user->host()."\"<br>\n";

//Create a second instance of User with a default host of "dbserver.com".
$user2 = new User('dbserver.com');

//This will echo the current value of "dbserver.com".
echo '$user2->host: "'.$user2->host()."\"<br>\n";
Sign up to request clarification or add additional context in comments.

Comments

2

For something like a db host, use a constant:

// define it first
define('DBHOST', 'localhost');

// then anywhere you can use DBHOST:
class User {
    function __construct() {
        echo DBHOST;
    }
}

http://php.net/manual/en/language.constants.php

2 Comments

Although correct, my recommendation is don't do this. Inject the values you need into the class.
That worked great, thanks. Didn't realize that would work inside the class.
1

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;
}
}

1 Comment

Thanks for all the ways and examples. I ended up using constants as I didn't want to have to pass variables with every instance and didn't want to call a setter function for every instance.
0

If you are planning to use variables (and not constants), then use the following code.

In config.php

$dbhost = "localhost";

In User.class.php

include 'config.php';    
class User {
    global $dbhost;
}

3 Comments

I wouldn't recommend using global variables, they can create bugs that are hard/impossible to track down.
Even I don't favor the use of global variables and hardly use them. But I tried to answer what the user asked specifically.
The OP doesn't mention global variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.