Sisäänkirjautuminen| Uusi tunnus
FrontPage| Projects| Forums| Search|
^ DaoGen Forums: › Why? › PHP5? » Using ToString() with PHP › Next DaoGen version almost ready! › Create (insert) and Save (update) PHP examples › Support for numeric object types almost ready! › DaoGen 2.1.0 released! › Compliments and minor suggestion › variables within a class › Foreign key constraint in create() › Security issue!!! › auto-generated columns and create method › Oracle BLOB › DAO for cross-table queries › Please help me.. › Please use this forum for DaoGen related stuff › Call-time Pass-By-Reference › DaoGen 2.4.0 has been released! › How can I show relationships? › DaoGen & PHP 5 › PHP4 vs PHP5 and other considerations. › singleQuery example in php › Why are save and delete not synchronized? › Why are save and delete not synchronized? › DTD for generated XML › DaoGen 2.2.2 has been released! › DaoGen 2.0.2 is available. › For a thesis ?? › Extremely good idea ?? how can I ???? › Usability suggestions for DaoGen › Performance of DaoGen with Mapping tools › Performance with List › Some usage statistics › DaoGen 2.2.1 has been released! › Programming Examples are back! › DaoGen 2.0.1 is available. › Version 2.0.0 generates buggy PHP code! New version soon available! › Examples of DaoGen customizations › Code Examples
Sisäänkirjautuminen
» Käyttäjätunnus:
» Salasana:
» Muista kirjautuminen:
Suosituimmat sivut: › DaoGen 2.1.0 released! » Using ToString() with PHP › DaoGen Forums › Programming Examples › DaoGen Manual › DaoGen FAQ › DaoGen › Projects › Forums › Search
FrontPage » Forums » DaoGen Forums » 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.