Jump to content

default functions in php?


voltaic

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.