在构造函数PHP上执行函数

i have class called dbconnection. I want to call this connection function in the constructor. Usually in Java, i just declare:

private FileName variable = new Filename(); 

and assign the variable.logOn on any functions.

But now, in authenticate php class, i couldn't do that. So i think i can execute the connection in constructor, I've tried:

public function __construct(){
  $conn = new Database();
}

but i couldn't access the $conn var because it's not global variable.

Another method i use is placing below code outside any function.

 public $conn  = new Database();

but it shows an error.

How properly calling the variable or executing another function from another class in php?

thanks.

You need to make $conn a field, and $this->conn to initialize it:

public $conn;  // declare $conn field

public function __construct () {
    $this->conn = new Database();  // initialize field
}

Do you mean like this?

public $conn;

public function __construct () {
    $this->conn = new Database();
}