You are here: Re: [PHP] Java - toString() <-> php - ? « PHP « IT news, forums, messages
Re: [PHP] Java - toString() <-> php - ?

Posted by Jochem Maas on 08/02/05 10:47

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
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация