I am making a minor CMS for specific use at my current job, and I want to do it right... hence OOP.
For this I want to make a database connection class that I can use in my other classes when I need to make database queries. However, for some reason, I keep being stopped at the same point, because I don't seem to be able to get the connection string into my classes.
Whatever changes I make, I end up with "undefined... " something every time.
File - databaseManagement.class.php:
class database {
private $user;
private $pass;
private $db;
private $serv;
private $type;
private $dsn;
private $sqlsrvString;
private $charset;
private $dbIni;
private $options;
public $connectionInfo;
public $dbConn;
public function __construct() {
$this->dbIni = parse_ini_file('settings/database.ini.php');
// ... assign values to individual variables based on values in the database.ini.php file.
$this->user = $this->dbIni['user'];
$this->pass = $this->dbIni['pass'];
$this->db = $this->dbIni['db'];
$this->serv = $this->dbIni['serv'];
$this->type = $this->dbIni['type'];
$this->charset = $this->dbIni['charset'];
$this->connectionInfo = array(
"Database"=>$this->db,
"UID"=>$this->user,
"PWD"=>$this->pass
);
}
public function dbConnect() {
$this->dbConn = sqlsrv_connect($this->serv, $this->connectionInfo);
}
File - smallInfo.class.php
class smallInfo {
function __construct() {
$initDB = new database();
$initDB->dbConnect();
var_dump($initDB);
}
function showDkProgress(){
echo "<hr />";
print_r($initDB->dbConn);
echo "<hr />";
}
Now the var_dump() in smallInfo constructor returns all the expected values, including those of $dbConn, however, I can't seem to access $dbConn in showDkProgress() for use in queries.
Two problems:
Constructors does not set variables (e.g.$variableName
) as object attributes. You have to explicitly assign it with$this->variableName
assignment.
Unlike Java, the OOP implementation in PHP does not automatically alias attributes. The variable$variableName
in a method does not automatically equal to$this->variableName
.
So:
class smallInfo {
function __construct() {
$this->initDB = new database();
$this->initDB->dbConnect();
var_dump($this->initDB);
}
function showDkProgress(){
echo "<hr />";
print_r($this->initDB->dbConn);
echo "<hr />";
}
}
In yoursmallinfo
class,initDB
is a local variable inside the constructor. It doesn't exists outside the constructor.
Thus, accessing an undefined variable namedinitDB
inshowDkProgress
yields an error.
You need to makeinitDB
a field.
class smallInfo {
private $initDB;
function __construct() {
$this->initDB = new database();
$this->initDB->dbConnect();
var_dump($this->initDB);
}
function showDkProgress(){
echo "<hr />";
print_r($this->initDB->dbConn);
echo "<hr />";
}
}
Function showDkProgress() is unable to get it since it is only local variable in constructor. If you embed $initDB in class you should add field for it.
so try eg:
class smallInfo {
public $initDB;
function __construct() {
$this->initDB = new database();
$this->initDB->dbConnect();
function showDkProgress(){
$si = new smallInfo();
echo "<hr />";
print_r($this->initDB->dbConn);
echo "<hr />";
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.