EDIT: Completely forgot about the programming thread. This can be locked, I will post it over in there.
Hi guys, I've been digging into PHP and I've begun to explore objects. I came across something that puzzled me and I begun reading up on it, but I"m still not clear:
Here is my current abstract class and child class setup:
abstract class dbInterface {
private $host = "host";
private $db = "db";
private $username = "unhere";
private $password = "passwordhere";
protected function __construct() {
$dbConnect = mysql_Connect($this->host,$this->username, $this->password);
if (!$dbConnect) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($this->db);
}
}
final class updatesObj extends dbInterface {
public $updateTitle;
public $updateTimeStamp;
public $updateContent;
public $errorMSG;
public function __construct($numUpdates) {
parent::__construct();
$this->openUpdatesTable($numUpdates);
}
...more code here
This works great and I've been having fun playing around with dynamic sites! However, I was talking to my friend at work who is a senior web developer and he mentioned that I should try out the mysqli class for handling all my database interactions. Curious I began rewriting my abstract class to instantiate a msqli object :
abstract class dbInterface {
private $host = "host";
private $db = "db";
private $username = "unhere";
private $password = "passwordhere";
protected function __construct() {
$mysqli = new mysqli($this->host,$this->username, $this->password, $this->db);
if ($mysqli->connect_errno) {
die("Connect failed: %s\n " . $mysqli->connect_error);
}
}
}
With that done, I had no idea how I could access methods of that object [since it's passed as a reference?] within the child class. Is this a logical way of going about this situation? If not, any suggestions for a better approach?
Thank you for your advice!
http://steamcommunity.com/id/aumni/ Battlenet: Aumni#1978 GW2: Aumni.1425 PSN: Aumnius
Posts
Subclasses can access super class properties that are 'protected' or 'public', but not 'private'.
Thank you clam. I'm going to have this thread closed and focus on the programming thread. THank you for the suggestion.
And it looks like this solution is going to work. Thank you!