473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 16999
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 Display Value 123.3400 123.34...
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, Object.ToString() is called instead. I've tried...
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 to the following Enum Monitor as Byte Small...
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" Namespace="DreamweaverCtrls"...
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 DateTime.ToString(), but no where can I find...
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() + ":" + DateTime.Now.Minute.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 from within jython, HelloPrinter.toString(hello)...
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 Visual Studio .NET 2003. What I am doing is adding...
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 for value objects that encapsulate a person's...
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 the behaviour couldn't be to return an empty...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.