The Little Guy Posted July 29, 2011 Share Posted July 29, 2011 I currently use __toString in my class, but some it only returns the same thing, how can I make toString return different things depending on what method called it? Quote Link to comment https://forums.phpfreaks.com/topic/243207-__tostring/ Share on other sites More sharing options...
requinix Posted July 29, 2011 Share Posted July 29, 2011 Don't. Use multiple methods. Or you might be able to use default arguments to simulate method overloading. Quote Link to comment https://forums.phpfreaks.com/topic/243207-__tostring/#findComment-1249123 Share on other sites More sharing options...
The Little Guy Posted July 29, 2011 Author Share Posted July 29, 2011 I guess when a method gets called I could set a private variable and set it to the current method. Is that a good or bad idea? What do you mean "Use multiple methods"? Quote Link to comment https://forums.phpfreaks.com/topic/243207-__tostring/#findComment-1249126 Share on other sites More sharing options...
ignace Posted July 30, 2011 Share Posted July 30, 2011 If this is for debugging purposes you should just extend your class to accept a log object and use that inside your class. $log->methodCalled(__METHOD__); Quote Link to comment https://forums.phpfreaks.com/topic/243207-__tostring/#findComment-1249370 Share on other sites More sharing options...
The Little Guy Posted July 30, 2011 Author Share Posted July 30, 2011 no, this isn't for debugging. I have two methods that return objects, and if you echo them out I want to know which on did so my __toString knows what to do. I am going to add more, that when echoed out use __toString, but they will return something completely different. The two current ones that use __toStirng return the same thing, which is a modified version of a GET query string (adding/removing key/value combinations). So what I have done, is set a private property called $function_name, and when ever a funciton name gets called (other than magic methods) it updates that property. So when __toString gets called I know what the last method that ran was. Here is a simplified example: <?php class MyClass{ private $function_name = null; private $hello = 'hello'; private $goodbye = 'goodbye'; public function __toString(){ switch($this->function_name){ case 'hello': return $this->hello; case 'goodbye': return $this->goodbye; } } public function hello(){ $this->function_name = __FUNCTION__; return $this; } public function goodbye(){ $this->function_name = __FUNCTION__; return $this; } } $mc = new MyClass(); echo $mc->hello(); // prints hello echo $mc->goodbye(); // prints goodbye echo $mc=>hello()->goodbye(); // prints goodbye echo $mc=>hello()->goodbye()->hello(); // prints hello ?> Quote Link to comment https://forums.phpfreaks.com/topic/243207-__tostring/#findComment-1249568 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.