Syntax error - parse_url (getenv ("CLEARDB_DATABASE_URL"));

1

I am trying to connect the bank of an application in PHP in herokuapp using ClearDB and I simply get a syntax error in line 6 of the code and I'm not sure exactly why. The application does not work, only if I put host , password and username directly, which is not possible since the application is in git too.

Follow the code:

    <?php

class Database {

    private $url = parse_url(getenv("CLEARDB_DATABASE_URL"));
    private $host = $url["host"];
    private $db_name = substr($url["path"], 1);
    private $username = $url["user"];
    private $password = $url["pass"];
    public $conn;

    public function getConnection() { 

            $this->conn = null;

            try {
                $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            } catch (PDOException $exception) {
                echo "Connection error: " . $exception->getMessage();
            }

            return $this->conn;
    }     
}

?>
    
asked by anonymous 18.01.2016 / 18:02

1 answer

0

Object properties must be instances with simple values defined at compile time and not determined at runtime like function calls and other types of expressions.

class Database {
   private $url = parse_url(getenv("CLEARDB_DATABASE_URL"));

Play this initialization in class constructor:

public function __construct(){
    $this->url =  parse_url(getenv("CLEARDB_DATABASE_URL"));
    $this->host = $this->url["host"];
    $this->db_name = substr($this->url["path"], 1);
    $this->username = $this->url["user"];
    $this->password = $this->url["pass"];
}

Property - Handbook

    
18.01.2016 / 18:14