TitanicLinux.Net :: Using ToString() with PHP



Using ToString() with PHP

Posted by jmaquino 30.04.2007 23:00

Hi everybody.
I've been using DaoGen as my basic tool for generating my Dao codes and it have saved me tons of time, now i'm using dao on PHP and I realized that the toString() function has a little bug, happens that the escape char '\n'does not work for split lines in html, so when you uses something like:
echo $myobject->toString();
you get a long line of code just like:
DaoGen version 2.4.1 class Democt, mapping to table democt Persistent attributes: id = 1 plate = abc123 driver = Pedro Perez hour = 2007-04-30 13:51:26
so i've modified the toString function to look like this:
function toString() {
$out = $this->getDaogenVersion();
$out = $out."
class Democt, mapping to table democt
";
$out = $out."Persistent attributes:
";
$out = $out."id = ".$this->id."
";
$out = $out."plate = ".$this->plate."
";
$out = $out."driver = ".$this->driver."
";
$out = $out."hour = ".$this->hour."
";
return $out;
}
I hope this modification can be of use fro someone and thanks lugi for that amazing tool.


Re: Using ToString() with PHP

Posted by anonymous 18.07.2007 02:54

why not just nl2br($object->toString())? http://php.net/nl2br


Re: Re: Using ToString() with PHP

Posted by jmaquino 04.02.2008 19:57

> anonymous 18.07.2007 02:54
> why not just nl2br($object->toString())? http://php.net/nl2br">http://php.net/nl2br">http://php.net/nl2br

I just don't know the function, its easier. thanks


Re: Using ToString() with PHP

Posted by anonymous 01.08.2008 10:02

yeah,i encounter this problem.


Re: Using ToString() with PHP

Posted by anonymous 05.08.2008 13:39

Or, if you are outputting to HTML, instead of converting a load of new lines to
tags... how about:

echo '
' . $myobject->toString() . '
;

just using the preformat tag which treats all whitespace as nonbreaking, tabs and newlines included


Re: Re: Using ToString() with PHP

Posted by anonymous 12.03.2009 18:54

The native method 'toString' in PHP is assigned as '__toString'. You can overload it in your classes to reproduces a correct conversion of your object to a string expression. i.e.

class myClass {

public function __toString() {
return "This is 'myClass', it overloads the 'toString' method."
}

}

$mc = new myClass();

echo $mc;


Re: Re: Re: Using ToString() with PHP

Posted by anonymous 27.05.2009 07:49

> $mc = new myClass();
>
> echo $mc;

Something I just noticed which is interesting.

Test program:

class Test { public function __toString() { return "Melons"; } }

$t = new Test();

print "Printing by itself:\n";
print $t;
print "\n\n";

print "Printing via concatenation:\n";
print $t."\n";
print "\n";

print "Printing via interpolation:\n";
print "$t\n";
print "\n";

print "Printing via sprintf:\n";
printf("%s\n", $t);
print "\n";

Outputs:

Printing by itself:
Melons

Printing via concatenation:
Object id #1

Printing via interpolation:
Object id #1

Printing via sprintf:
Object id #1

So it would appear that __toString() is only automatically called in this one case where the item is printed by itself. As soon as you concatenate it using any of the three methods I tried, it stops using the class's custom __toString() and falls back to the default one for objects.



viimeisin päivitys: 30.06.2009 16:47 Author: TitanicLinux.Net
Copyright TitanicLinux.Net 2003-2010