CaptainChainsaw Posted May 12, 2009 Share Posted May 12, 2009 Hi all, I have a problem with my code, it does work but I'm a bit concerned that it is working, as I expect that it shouldn't be. All the classes in my code have a private member called $members=array(); which I would expect to be private to that class. When I run the following code................ $orders=new orders(); $data=new cleanser(); foreach($orders as $order){ $data->__set('Order', $order); $data->clean(); $filedata = $order->getName()."\t".$order->getAddress(); } ............. the data is cleansed and properly assigned to $filedata. What I'm wondering is shouldn't I need to call: $data->__get('Order'); in order to "get" the cleansed object? When calling $order->getName(); why would the "Name" be displayed as I expect it if the cleansed object hasn't been returned? Have I done something wrong if I don't need to call the __get() method? I *think* I've designed my classes well enough but I could be wrong. I know it would probably help to have the classes posted here but I don't have access to them right now. Cheers for any tips or suggestions. CaptainChainsaw Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/ Share on other sites More sharing options...
Zhadus Posted May 12, 2009 Share Posted May 12, 2009 I understand you don't have access to the classes, but what is getName() structured like? Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-832614 Share on other sites More sharing options...
allworknoplay Posted May 12, 2009 Share Posted May 12, 2009 It sounds like getName is your "get" method, which allows you to access private properties in a class I beleive. I could be wrong... I don't think you can directly access those properties like so: $data->members(); But if your getName() is getting the data and then outputting it, that's probably why you're seeing the data?? Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-832620 Share on other sites More sharing options...
CaptainChainsaw Posted May 12, 2009 Author Share Posted May 12, 2009 Hi all, thanks for your replies. As far as I can remember the "getName()" method is structured like so: public function getName(){ return $this->members['Order']['name']; } As the modified $orders object hasn't been returned by the $data object why is it showing the "cleansed" data? I would expect echo $order->getName(); to show the unmodified data as the object has only been set and "cleaned" by the $data object and NOT returned. Hope that makes a little more sense (if you needed it). Cheers again, CaptainChainsaw Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-832666 Share on other sites More sharing options...
Maq Posted May 12, 2009 Share Posted May 12, 2009 You should be requiring all interaction to be performed through an object's methods. They are 'get' and 'set' methods (accessors and mutators) in your class that get and set data. From the information provided it looks like your code obeys the rules of encapsulation. Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-832712 Share on other sites More sharing options...
CaptainChainsaw Posted May 12, 2009 Author Share Posted May 12, 2009 Hi Maq, I agree, all interaction is done with getters and setters, I just don't understand why I don't need to do something like this within the loop: $data->__set('Order', $order); $data->clean(); $order=$data->__get('Order'); hmmm ..... Ah well at least my code works! Thanks again, CaptainChainsaw Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-832729 Share on other sites More sharing options...
edwardoka Posted May 13, 2009 Share Posted May 13, 2009 Looks like PHP is passing your $order object by reference to the $data class. Some versions of PHP will do this implicitly I believe. And yes, you are violating the laws of encapsulation. (not really) Ed Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-833084 Share on other sites More sharing options...
Maq Posted May 13, 2009 Share Posted May 13, 2009 Looks like PHP is passing your $order object by reference to the $data class. Some versions of PHP will do this implicitly I believe. And yes, you are violating the laws of encapsulation. (not really) Ed Care to expand? Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-833179 Share on other sites More sharing options...
edwardoka Posted May 13, 2009 Share Posted May 13, 2009 Not really, no As per http://www.php.net/manual/en/migration5.oop.php, objects are passed about by reference instead of by value. CaptainChainsaw was wondering why his code was working when at a glance it appears that it shouldn't. Cheers Ed Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-833272 Share on other sites More sharing options...
CaptainChainsaw Posted May 13, 2009 Author Share Posted May 13, 2009 Cheers Ed, that explains it! Quote Link to comment https://forums.phpfreaks.com/topic/157855-solved-am-i-violating-the-laws-of-encapsulation/#findComment-833449 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.