Re: Java - toString() <-> php - ?

From: Date: Tue, 02 Aug 2005 07:47:30 +0000
Subject: Re: Java - toString() <-> php - ?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-219814@lists.php.net to get a copy of this message
Adi Zebic wrote:
To print "state" of object in Java I can define toString() function and then I can do something like this: Java exemple: ************************************* import java.io.*; import java.util.*; public class ReadTextFromFile {
    private static String line;
    private static String Fable = new String ("");
    private static String NomFable;
    private static int cptLine = 0;
    private static int nbrPersonnes = 0;
    private static String nomPersonnes = new String ("");
    private static ArrayList AListe = new ArrayList(nbrPersonnes);
//********************************************************************************** public String toString() {
    return getClass().getName()
    + " [ Nom de la fable: "+  NomFable
    + "  <==>  Nombre de personnes dans dialogue: " + nbrPersonnes
    + " ] "
    ;
} *********************************************************************************** Is there similar kind of function in php that help us to print the state of object in his "life evolution"?
there is but it's use is limited and currently AFAIR it only works on internally defined classes.... the method is called __toString() - from what I have read and played with it (not recently though) the fucntionality is _bit_ 'halfbaked' - as in the internals guys are still pondering how to imnplement this as nicely as possible. an example: <? class Test { function __toString() { return "testing 1,2,3\n"; } } $t = new Test; echo $t; print($t); ?> the problem with implementing this is the fact that php is dynamically typed - and it autocasts types all over the place - very handy BUT it makes a __toString() [magic] method very hard to implement in a way that is obvious to the people using it (try playing around with the SimpleXML extension to see how it can warp your brain! well it makes my head spin anyway :-) but don't take my word for it - try it yourself, oh and also take a dive into the [php]internals mailing list archives - lots has been discussed about this function AFAIR.
Thanks ADI


« previous php.general (#219814) next »