voltaic Posted December 18, 2007 Share Posted December 18, 2007 i am somewhat new to php, and was wondering how abstract data types know how to display themselves? for example, in java there a method called totext (i think that was it's name) that is the default method called when an object was output to a text stream. i notice that things like arrays in php just "know" how to display themselves, which occurs to me to not be intrinsic to abstract data types. is there such a setup in php as there is in java, or does php just do it's best to come up with a suitable way to display abstract objects? Quote Link to comment Share on other sites More sharing options...
redbullmarky Posted December 18, 2007 Share Posted December 18, 2007 i can't speak for arrays other than to say that displaying an array literally outputs 'Array' unless you're referring directly to one of its elements: $names = array('male'=>'Bob', 'female'=>'Sue'); echo $names; // outputs 'Array' echo $names['male']; // outputs 'Bob' with objects, there's a fair few magic methods that handle what you're on about: http://uk2.php.net/oop5.magic the __toString one being the one you mention: The __toString method allows a class to decide how it will react when it is converted to a string. hope that helps Quote Link to comment Share on other sites More sharing options...
Liquid Fire Posted December 18, 2007 Share Posted December 18, 2007 also, print_r() will print out the array in a readable format. print_r($names); will print out everything in an array. Quote Link to comment Share on other sites More sharing options...
voltaic Posted December 20, 2007 Author Share Posted December 20, 2007 yup, i was confusing using the echo/print commands with the print_r command. anyway, its nice to know that fuctionality exists. thanks! 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.