Sajan Parikh
Sajan Parikh

Reputation: 4950

How to use an outside variable in a PHP class?

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.

Upvotes: 3

Views: 399

Answers (4)

Jordan Mack
Jordan Mack

Reputation: 8733

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

Upvotes: 4

Songo
Songo

Reputation: 5736

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

Upvotes: 1

vivek
vivek

Reputation: 2004

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

Upvotes: 0

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

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

Upvotes: 2

Related Questions