class Conn // No parent db class - it serves no purpose
// Class names begin with capitals, by convention
{
private $host; // Local copies of database credentials. Not really needed
private $user;
private $pass;
private $dbname;
// Set up the database connection in the constructor.
public function __construct(string $host, string $user, string $password, string $dbname) {
$this->host = $host;
$this->user = $user;
$this->pass = $password;
$this->dbname = $dbname;
// Set mysqli to throw exceptions on error
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// open a connection for later use
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
}
}
class Work extends Conn
{
public function insert_user(string $email, string $password_hash)
{
$sql = "INSERT INTO user (email,password) VALUES (?,?)";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param('ss', $email, $password_hash);
$stmt->execute();
return;
}
}
try {
$obj = new Work("server", "username", "password", "schema");
$obj->insert_user('user@example.com', 'asdfasdf');
echo "inserted";
} catch (Exception $e) {
echo $e->getMessage();
}
I have this code in class.php file . How can I use this page $obj in another file. Like I want to use $obj in prcess.php file. How can I do it now?