Liquid Fire Posted November 8, 2007 Share Posted November 8, 2007 Ok, i am trying to start a framework and a big part of it is going to be the base_data class which will have common functions for objects the is retrived from or saved to a database. here is code to help explain my question: base_data class: <?php class base_data { private function set_data($data, $value) { $this->$data = $value; } } ?> application class extends base_data <?php public function __call($function, $parameters) { list($function, $data) = explode("_", $function, 2); switch($function) { case "set": $value = $parameters[0]; parent::set_data($data, $value); return true; break; case "get": return $this->$data; break; default: return false; break; } } private $id; ?> Now i am using parent::set_data() because i have always seen it used that way(where is C++ i would normally do this->set_data or php version $this->set_data). Now with this code would parent::set_data know about $id in application, my guess is no? if not can i call set_data like $this->set_data and then will it know about $id? If both are false the i would have to pass a reference of the object to complete this functionality which seems very un-OOP like. Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 Calling parent::set_data() will give you access to variables in the extended class, but not with the scope that you have them at. Variables and methods marked as 'private' are ONLY accessible through the class they reside in. If you want other classes that extend your class to access them, you need to mark them as 'protected'. Marking them as 'public' will obviously let anything use those methods/variables, regardless of whether or not they extend the class or not. At minimum to make your example work, you would need to mark set_data() as protected in the base_data class, and $id as protected in the application class. Here's a simplified example of how you can get it to work. <?php class base { protected function set_data($data) { echo $this->id; } } class application extends base { protected $id = 14; public function set($value) { parent::set_data($value); } } $class = new application(); $class->set('test'); ?> Quote Link to comment Share on other sites More sharing options...
Liquid Fire Posted November 8, 2007 Author Share Posted November 8, 2007 what if i keeped the private and did this <?php public function __call($function, $parameters) { list($function, $data) = explode("_", $function, 2); switch($function) { case "set": $value = $parameters[0]; $this->set_data($data, $value); return true; break; case "get": return $this->$data; break; default: return false; break; } } private $id; ?> Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 No, that won't work. Something marked as private can only be accessed within the class it is defined in. No other class can access it, even if it extends it. Marking them as protected is necessary to allow classes that extend your base class access to variables and methods in other classes. I would suggest reading up on object member visibility if you are still a bit unclear as to the difference between private, protected, and public. It's in the php manual here: http://www.php.net/manual/en/language.oop5.visibility.php Quote Link to comment Share on other sites More sharing options...
Liquid Fire Posted November 8, 2007 Author Share Posted November 8, 2007 Your right, kinda of a brain fart had. i do need to use protected. My question is there a difference from parent::set_data() and $this->set_data() From what i know about OOP(which i think is pretty good) the parent::set_data would only have access to the members inside the parent class but $this->set_data i think woudl have access to the child class since i am calling it from the child class. Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 Either way will have access to everything listed as protected or public in either class. It doesn't matter which way you call it. The only real difference between calling $this->set_data() and parent::set_data() is that if you defined a method called set_data() in the extended application class, that would get called instead of the set_data() in the base class. Using parent::set_data() ensures that the set_data() method in the base class is always called, regardless of whether or not you created a function of the same name in the extended class. Try this example to see what I mean. <?php class base { protected function set_data() { echo "I'm in the base class<br />"; } } class application extends base { public function set() { // call to the parent class parent::set_data(); // call to the current class $this->set_data(); } protected function set_data() { echo "I'm in the application class<br />"; } } $class = new application(); $class->set(); ?> Quote Link to comment 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.