473,890 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

toString() in PHP

Dheeraj Joshi
1,123 Recognized Expert Top Contributor
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 17022
Markus
6,050 Recognized Expert Expert
@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 Recognized Expert Top Contributor
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::__tostr ing();
Wont work..


Any suggetions?
Jul 22 '09 #3
Markus
6,050 Recognized Expert Expert
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 Recognized Expert Top Contributor
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 Recognized Expert Expert
@dheerajjoshim
Why is it not?

Mark.
Jul 22 '09 #6
Atli
5,058 Recognized Expert Expert
@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
3948
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 123.0000 123 i.e. I want to trim trailing zeros and (decimal point if no decimal values
6
4682
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 everything: Public Overrides Function ToString() as String, Overrides Function ToString() as String, Overrides Overloads ToString() as string, Shadows ToString() as String,
8
1737
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 Medium Large End Enum
0
2381
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" Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,culture=neutral" %> <MM:DataSet id="SSF2Pinky"
7
7951
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 information on ordinals. For example, I currently use: Console.WriteLine(myDate.ToString("d MMMM, yyyy"));
2
7519
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() + ":" + DateTime.Now.Second.ToString() + ":"+DateTime.Now.Millisecond.ToString() to the file.
1
4666
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) call gives results like Object.toString() of hello has being called. The example below shows this behaviour. Could somebody explain this?
8
2844
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 objects which are derived from CultureInfo to a ListBox. I want the language name to be displayed in the native language, so I override the ToString() method to access the CultureInfo.NativeName
2
1847
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 Name, for example. Would anyone write a custom format provider to display the last name first ("Phelps, Michael"), or first name first ("Michael Phelps")? Or use the familiar name instead ("Mike"). Would you use some other technique? Would you...
19
2730
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 string. When we call x.ToString we are really calling a function like this: class Object
0
9819
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10810
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4674
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3275
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.