3raser Posted April 6, 2012 Share Posted April 6, 2012 I have a user class that is very dependent on the database class, which is why the user class extends the database. I tried creating a protected method in the parent class called getDBCObject, which returned the database object/variable/handle that I want the user to have access to. I tried the method below, but it doesn't work: <?php /* * @DATABASE * ~~~~~~~~~~~~ * @FILE DESCRIPTION: Handles all database related processes * @LAST MODIFIED: April 4, 2012 */ class database { protected $dbc; function __construct($db_host, $db_name, $db_user, $db_password) { try { $this->dbc = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password); } catch(PDOException $e) { echo '<b>An error occured while trying to create a database connection: </b>'. $e->getMessage(); } } /* * @METHOD getDBCObject * @DESC Gives the $dbc object/variable to its child classes */ protected function getDBCObject() { return $this->dbc; } } ?> My user class: <?php /* * @DATABASE * ~~~~~~~~~~~~ * @FILE DESCRIPTION: User related proccess * @LAST MODIFIED: April 5, 2012 */ class user extends database { protected $dbc; public function __construct() { if(parent::getDBCObject() == null) { echo '<br/>A database class/connection is required before creating the user class.'; } } public function isLoggedIn() { if($_COOKIE['user']) { //soon to come } else { return false; } } } ?> Any feedback on how I can let the user class use the $dbc variable in the database class? Quote Link to comment https://forums.phpfreaks.com/topic/260438-how-to-let-a-child-access-parents-variables/ Share on other sites More sharing options...
requinix Posted April 6, 2012 Share Posted April 6, 2012 Wrong syntax. Use $this-> like you would for most everything else. But I have a user class that is very dependent on the database class, which is why the user class extends the database. No. That's not what inheritance is about. The only time you would do this is if the user was a type of database. Which it isn't. If the user class needs access to database stuff then give it access to database stuff. Don't try to force an inheritance hierarchy that doesn't make sense. Quote Link to comment https://forums.phpfreaks.com/topic/260438-how-to-let-a-child-access-parents-variables/#findComment-1334884 Share on other sites More sharing options...
trq Posted April 6, 2012 Share Posted April 6, 2012 A *user* is not a *type* of database so the relationship makes no sense. A user might depend on a database, but that is as far as the relationship should stretch. As for your issue: Firstly, you reference methods and properties declared within a parent class using $this not parent. Just as you would any local method or property. The only time you need to use *parent* is when you are overriding a parents method and within that child method you need to call the original parent method. Secondly, you have not implemented the child __construct in such a mannor that you will be able to create a database object. These two points are mute however as like I already stated, user should not be extending database. Quote Link to comment https://forums.phpfreaks.com/topic/260438-how-to-let-a-child-access-parents-variables/#findComment-1334885 Share on other sites More sharing options...
3raser Posted April 6, 2012 Author Share Posted April 6, 2012 Hmm, I understand what you're saying. But do you have any suggestions how I can allow the user access class access to the database without creating two different instances of the database class? Such as on index.php, it creates a database object - how would the class also work with that object/instance of the database class without creating another one of its own? I hope I've worded that in an understandable manner. :/ Quote Link to comment https://forums.phpfreaks.com/topic/260438-how-to-let-a-child-access-parents-variables/#findComment-1334887 Share on other sites More sharing options...
trq Posted April 6, 2012 Share Posted April 6, 2012 Pass the database object into the construct of the user object. Quote Link to comment https://forums.phpfreaks.com/topic/260438-how-to-let-a-child-access-parents-variables/#findComment-1334893 Share on other sites More sharing options...
3raser Posted April 6, 2012 Author Share Posted April 6, 2012 Pass the database object into the construct of the user object. *facepalm* Thank you, Thorpe. Quote Link to comment https://forums.phpfreaks.com/topic/260438-how-to-let-a-child-access-parents-variables/#findComment-1334894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.