472,330 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

toString() in PHP

Dheeraj Joshi
1,123 Expert 1GB
I was wondering, is there any method as toString() to convert a object to string?

As in JAVA???

Regards
Dheeraj
Jul 22 '09 #1
6 16925
Markus
6,050 Expert 4TB
@dheerajjoshim
PHP has the magic-method __toString(), that can be used to present a textual representation of the object.

Expand|Select|Wrap|Line Numbers
  1. class toString {
  2.     public function __toString() {
  3.         return "Object toString";
  4.     }
  5. }
  6.  
  7.  
  8. $ts = new toString;
  9. printf("toString::__toString() - %s", $ts);
  10. // echo
  11. echo $ts;
  12.  
Jul 22 '09 #2
Dheeraj Joshi
1,123 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class Style {
  3.  
  4.     var $text;
  5. function Style ($text="#000000",)
  6. {
  7.  $this->text=$text;
  8. }
  9. }
  10. ?>
  11.  
  12. <?php $Basic = new Style; ?>
Now i want to convert this object to String

$Basic::__tostring();
Wont work..


Any suggetions?
Jul 22 '09 #3
Markus
6,050 Expert 4TB
Your class must implement the __toString() method - you don't call it yourself. Look again at my example.
Jul 22 '09 #4
Dheeraj Joshi
1,123 Expert 1GB
Yup a added that and got the output.

But it is not similar to object.toString() in java right?
Jul 22 '09 #5
Markus
6,050 Expert 4TB
@dheerajjoshim
Why is it not?

Mark.
Jul 22 '09 #6
Atli
5,058 Expert 4TB
@dheerajjoshim
The only difference, really, is in the spelling.
Both serve to provide a string representation of the object, and both need to be implemented specifically for an object to provide an accurate representation. (Otherwise you just get a generic "object" string, or nothing at all)

But you should keep in mind that PHP and Java are two extremely different languages, and things may not be implemented the same way between the two, if they are implemented at all.
Jul 22 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value ...
6
by: Joe | last post by:
Stupid question, but I'm really stuck I have a class that overrides ToString(). When this class is cast back to Object, and ToString() called,...
8
by: John Cobb | last post by:
Excuse me if I use some terminology incorrectly as I am not well versed in Object Orientation. In short I want to create an Enumerated type similar...
0
by: bcobra | last post by:
What am I doing wrong? the code: age Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Register TagPrefix="MM"...
7
by: Dylan Parry | last post by:
Hi folks, I was wondering if there is any way of formatting a date to include the ordinal characters? I've looked at the documentation for...
2
by: archana | last post by:
Hi all, I am facing some wired problem while using above mention data type. What i am doing is i am writing DateTime.Now.Hour.ToString() + ":"...
1
by: ivansh | last post by:
Hello, For one java class (Hello) i use another (HelloPrinter) to build the string representation of the first one. When i've tried to use this...
8
by: Phil Jollans | last post by:
Hi, I am having difficulty overriding the ToString() method of CultureInfo using Visual Studio 2005. Exactly the same code works fine with...
2
by: Berryl Hesh | last post by:
I'm interested in how experienced.Net developers might handle routine display tasks as a general strategy. Let's say you have a use in your domain...
19
by: Michael C | last post by:
If we have something like this object x = null; MessageBox.Show(x.ToString()) we get an error. That kindof makes sense but there is no reason...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.