Lamez Posted December 10, 2010 Share Posted December 10, 2010 I am working on my Pool class (base) and my FootballPool class (derived). I have this as my constructor Pool.php protected $pid, $uid, $pkid, $name; function __construct($pid, $uid){ $this->pid = $pid; $this->uid = $uid; $this->pkid = $this->pkid(); if(isset($_SESSION['picksInfo']['name'])) $this->name = $_SESSION['picksInfo']['name']; else $this->name = NULL; } Now a function in my FootballPool class needs to call $pid, $uid, and $pkid. How can I do that? I have tried this: Pool::$pid, but then I get this error Fatal error: Access to undeclared static property: Pool::$pid in /var/www/core/includes/FootballPool.php on line 31 I am confused, because in the parents constructor, it is set. So basically, how can I call a variable set in the base class from a child or derived class? Quote Link to comment https://forums.phpfreaks.com/topic/221237-question-about-classes/ Share on other sites More sharing options...
OOP Posted December 10, 2010 Share Posted December 10, 2010 IF FootballPool extends the Pool class then you can simple call the variable like this $this->pid; Quote Link to comment https://forums.phpfreaks.com/topic/221237-question-about-classes/#findComment-1145456 Share on other sites More sharing options...
Anti-Moronic Posted December 10, 2010 Share Posted December 10, 2010 You can't access those statically. this :: is used to access static 'things'. However, you need to access these variables as an object as that is how they are set. Pool::$pid should be $this->pid; ..if used within your extended class. Quote Link to comment https://forums.phpfreaks.com/topic/221237-question-about-classes/#findComment-1145458 Share on other sites More sharing options...
Lamez Posted December 10, 2010 Author Share Posted December 10, 2010 @OOP: Okay, I was having trouble a while ago, but I had the variables set as private, then I was getting an error saying that it was not a property of FootballPool. I just tried what you suggested and it worked. So if in the base class, the property that I am trying to access has to be protected and above? If private, it only belongs to the base, or that class? Quote Link to comment https://forums.phpfreaks.com/topic/221237-question-about-classes/#findComment-1145460 Share on other sites More sharing options...
Anti-Moronic Posted December 10, 2010 Share Posted December 10, 2010 @OOP: Okay, I was having trouble a while ago, but I had the variables set as private, then I was getting an error saying that it was not a property of FootballPool. I just tried what you suggested and it worked. So if in the base class, the property that I am trying to access has to be protected and above? If private, it only belongs to the base, or that class? If it is private, only that class can access it. This rings true for methods too. To access private variables of a class outside that class you have to use a 'getter' like so: function getPid(){ return $this->pid; } Then in your extended class or really anywhere now after you have instantiated the object: $this->getPid(); Quote Link to comment https://forums.phpfreaks.com/topic/221237-question-about-classes/#findComment-1145469 Share on other sites More sharing options...
Lamez Posted December 10, 2010 Author Share Posted December 10, 2010 lol I have never heard anyone call it a 'getter' before. I know what you mean, an accessor. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/221237-question-about-classes/#findComment-1145488 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.