I am beginning to learn php
, I am trying to make a connection to the database with which I have to work, (Oracle SQL developer), because the following happens, I have two codes: Pruebas.php
,idex.php
in Tests :
class conectar{
public static function conex(){
try{
$conex= new PDO('oci:dbname="Aqui Coloco la IP de la BD"/orcl;charset=UTF8', 'usuario', 'contraseña');
}
catch(Exception $e){
die("Error". $e->getMessage());
echo "Linea de Error" . $e->getLine();
}
return $conex;
}
}
and in index :
require("Pruebas.php");
$x = new conectar::conectar();
var_dump($x);
The error it throws is the following:
Parse error: syntax error, unexpected 'connect' (T_STRING), expecting variable (T_VARIABLE) or '$' in C:\wamp64\www\PHP Tests\index.php on line 5
You have two problems:
On the one hand, you are trying to make an
new
envelope of the result of a static method call. That is not right. your line:It is incorrect, since the static method of your class itself returns a result (in this case, it will be the connection object). You don't have to instantiate the object with new, because the method itself will return it to you.
On the other hand, the method is called
conex
, so it gives you the error of the stringconectar
as method, which does not exist.In your case, you can solve it by changing the previous line to: