1

Config.php

class Config {
    public static $dbserver = "hostedserverURL";
}

DB.php

require 'Config.php'
class DB {

    private $server =  Config::$dbserver; // compile-error
    private $user = "user";
    private $password =  "password";
    private $database =  "databasename";
    private $db;
}

compile-error says "syntax error, unexpected '$dbserver', expecting 'identifier' or 'class'"

If I remove the $ and change the line to private $server = Config::dbserver; , the compile-error is gone. but this is not correct. I get a Runtime error in that case. Fatal error: Undefined class constant 'Config::dbserver' in ..

So I have to retain the $ , Also as per this SO thread: Fatal error: Undefined class constant

This is where I am making use of it,

public function __construct()
    {
        $this->db = new PDO(
            "mysql:host={$this->server};dbname={$this->database};charset=utf8",
            $this->user,
            $this->password
        );
        return $this;
    }

Question: How can I refer the static variable dbserver and make that as default value for $server of class DB ? Any ideas please

1
  • 1
    Only available as of PHP 5.6 for some part Commented Jan 18, 2016 at 22:14

3 Answers 3

3

You can not assign variables in classes from functions and other classes or non-trivial expressions until 5.6. You will have to set the variable using a function within the class.

For now type in php -v into your terminal to see what version you are using. Otherwise if you want to use that functionality, upgrade PHP to PHP 5.6

https://wiki.php.net/rfc/const_scalar_exprs

Sign up to request clarification or add additional context in comments.

Comments

2

This is a php (version < 5.6) limitation, however you can simply initialize the properties in the constructor:

class DB 
{

    private $server;
    private $user;
    private $password;
    private $database;
    private $db;

    public function __construct()
    {
        $this->server   = Config::$dbserver; // No compile-error
        $this->user     = "user";
        $this->password = "password";
        $this->database = "databasename";
        $this->db       = new PDO(
            "mysql:host={$this->server};dbname={$this->database};charset=utf8",
            $this->user,
            $this->password
        );
        //return $this; no need for this
    }
}

Or upgrade to a later (5.6+) version of php.

Also, from a design perspective, having the various variables hard-coded and scattered over 2 files is needlesy complicated. Ideally have them all injected in the constructor:

public function__construct($server, $user, $password, $database)
{
    $this->server = $server; 
    $this->user   = $user;
    //etc
} 

failing that have them all declared in the config class:

public function__construct()
{
    $this->server = Config::$server; 
    $this->user   = Config::$user;
    //etc
} 

4 Comments

Thank you Steve, I'm trying this now
@JavaDev No problem. You should also look into class autoloading, its very simple to set up and will remove the need to add require or include all over the place.
Additionally, having the various variables hard-coded and scattered over 2 files is needlesy complicated. Ideally have them all injected in the constructor, eg __construct($server, $user, $password, $database){$this->server = $server; $this->user = $user;//etc} failing that have them all declared in the config class
Thanks again Steve, I tried the second approach and it works. Somehow I feel not to go with first approach of injecting in the constructor because then the responsibility is delegated to the caller. Also +1 again [which I cannot do in SO:)] for class autoloading concept.
1

You can't use variables from outside the class, even if you require them in the same file.

You could do this:

class DB {
    private $server;
    private $user = "user";
    private $password =  "password";
    private $database =  "databasename";
    private $db;  

    public function __construct(){
       require 'Config.php';
       $this->server = Config::$dbserver; //Will set the $server variable of the instantiated class.
    }
}

5 Comments

Thanks, +1, I can do this and make it work , but everytime the constructor is called, isn't there an extra burden of require 'Config.php' everytime?
Well everytime you include the file with the class you would load the Config.php anyways... Calling it in the constructor is the same but in this way it works.
@PhiterFernandes whilst there are no adverse effects from this, there is also no advantage over adding your require at the top of the file, plus it makes it harder to read the classes dependants
Other solution would be to pass the $dbserver as a parameter on the constructor... so you don't need to call the file inside the constructor itself, but yes in the file calling the class. If it is of your desire, I can edit the question with an example.
Thank you @PhiterFernandes for providing the answer at the first shot, Actually What I realized now is that require 'Config.php'; need not be included inside the constructor. It can be a top-level declaration in the file.

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.