As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

[RESOLVED]PHP Parent/Child Object behavior

AumniAumni Registered User regular
edited July 2011 in Help / Advice Forum
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
Aumni on

Posts

  • Options
    clam2000clam2000 SeattleRegistered User regular
    In case you still need an answer - you probably want to save your reference to $mysqli as a property on the abstract class, which you assign in the constructor.

    Subclasses can access super class properties that are 'protected' or 'public', but not 'private'.

    cdogwal.gif
  • Options
    AumniAumni Registered User regular
    edited July 2011
    clam2000 wrote:
    In case you still need an answer - you probably want to save your reference to $mysqli as a property on the abstract class, which you assign in the constructor.

    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!

    Aumni on
    http://steamcommunity.com/id/aumni/ Battlenet: Aumni#1978 GW2: Aumni.1425 PSN: Aumnius
This discussion has been closed.