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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.