Problems with PDO and PHP

0

I need to connect to a db oracle, for that I'm using the following code!

<?php  class ConnectionDBTOAD{
private static $conn=null;
private static $server = "mi host";
private static $db_username = "mi usuario";
private static $db_password = "contraseña";
private static $service_name = "serviciox";
private static $sid   = "serviciox";
private static $port  = 1521;
private static $dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";
public static function OpenBDTOAD(){
    if (self::$conn==null) {
        try{
            self::$conn = new PDO("oci:dbname=" . self::$dbtns . ";charset=utf8", self::$db_username, self::$db_password, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
        } 
        catch (PDOException $e) {
            echo $e->getMessage();
        }
        return self::$conn;
    }
}
public static function CloseBDTOAD(){
    self::$conn=null;
}}?>

However, in the browser I get the message

  

Fatal error: Constant expression contains invalid operations in C: \ xampp \ htdocs \ cycle \ model \ connectiondt.php on line 9

that is, the error is right on this line

private static $dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";

I have already modified the php.ini file by enabling

  

extension = php_pdo_oci.dll

I have also tried to place the connection outside of a class and I get the same error!

    
asked by accountflank 27.04.2017 в 15:14
source

1 answer

0

The problem

Unable to create a static property ( eg: $dbtns ) in classes that depend on others, since this implies a operación to calculate the value of the same. Also, it is worth mentioning that the variables $server, $port, etc. that are within the string do not exist .

Quote from the PHP manual

  

Static properties can only be initialized using a literal string or a constant; the expressions are not allowed. Therefore, you can initialize a static property with integers or arrays (for example), but it can not be done with another variable, with the return value of a function, or with an object.

Solution:

Assign the calculated value to the variable $dbtns when executing the OpenBDTOAD method.

Example:

<?php
class ConnectionDBTOAD
{
    private static $conn = null;
    private static $server = "mi host";
    private static $db_username = "mi usuario";
    private static $db_password = "contraseña";
    private static $service_name = "serviciox";
    private static $sid = "serviciox";
    private static $port = 1521;
    private static $dbtns;
    public static function OpenBDTOAD()
    {
        if (self::$conn == null) {

            // AQUI - Asignamos el valor
            self::$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = " . self::$server . ")(PORT = " . self::$port . ")) (CONNECT_DATA = (SERVICE_NAME = " . self::$service_name . ") (SID = " . self::$sid . ")))";
            try {
                self::$conn = new PDO("oci:dbname=" . self::$dbtns . ";charset=utf8", self::$db_username, self::$db_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
            return self::$conn;
        }
    }
    public static function CloseBDTOAD()
    {
        self::$conn = null;
    }
}
    
answered by 27.04.2017 / 16:55
source