object-oriented question
- by user522962
I am attempting to put all my database connections in 1 php file, rather than in each of my individual php pages. I have the following:
//conn.php:
<?php
class conn {
var $username = "name";
var $password = "password";
var $server = "localhost";
var $port = "3306";
var $databasename = "db";
var $tablename = "tablename";
var $connection;
public function getConnected() {
$this->connection = mysqli_connect(
$this->server,
$this->username,
$this->password,
$this->databasename,
$this->port
);
}
}
?>
// file.php:
<?php
require_once("conn.php");
class myClass{
public function con() {
$conn = new conn();
$conn->getConnected();
}
public function myF() {
$stmt = mysqli_prepare($conn->connection, "SELECT * FROM $conn->tablename");
mysqli_stmt_execute($stmt);
}
}
?>
I then call this as follows:
$myNew = new myClass();
$myNew-con();
$myNew-myF();
When I call this, I get the following error:
Undefined property: myClass::$connection
What am I doing wrong?