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? Link to comment https://forums.phpfreaks.com/topic/82154-default-functions-in-php/ 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 Link to comment https://forums.phpfreaks.com/topic/82154-default-functions-in-php/#findComment-417482 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. Link to comment https://forums.phpfreaks.com/topic/82154-default-functions-in-php/#findComment-417587 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! Link to comment https://forums.phpfreaks.com/topic/82154-default-functions-in-php/#findComment-419785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.