473,856 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object of class Person could not be converted to string

Code which should allow my constructor to accept arguments:

<?php
class Person {
function __construct($na me)
{
$this->name = $name;
}

function getName()
{
return $this->name;
}

function printName()
{
print $this->name;
}

private $name;
}

$judy = new Person("Judy") . "\n"; // <- this is line parser don't
like
$joe = new Person("Joe") . "\n";

$judy->printName() . '<br />';
$joe->printName() . '<br />';
?>

Outputs:

Catchable fatal error: Object of class Person could not be converted
to string

Anyone seen this before? Know why? Am I missing something here?

Thanks all...

Gene Kelley
LAMP Web Developer
bizFlowDesigns. com

Jun 11 '07 #1
21 55651
At Mon, 11 Jun 2007 04:46:57 +0000, phpCodeHead let h(is|er) monkeys type:
Code which should allow my constructor to accept arguments:

<?php
class Person {
function __construct($na me)
{
$this->name = $name;
}

function getName()
{
return $this->name;
}

function printName()
{
print $this->name;
}

private $name;
}

$judy = new Person("Judy") . "\n"; // <- this is line parser don't
like
$joe = new Person("Joe") . "\n";

$judy->printName() . '<br />';
$joe->printName() . '<br />';
?>

Outputs:

Catchable fatal error: Object of class Person could not be converted
to string

Anyone seen this before? Know why? Am I missing something here?

Thanks all...

Gene Kelley
What are you trying to achieve by concatenating "\n" to an object?
Your constructor creates an instance of your class, not a string.
Since you then concatenate it with ."\n" PHP attempts to convert the
object to a string, and fails. If you want that, you have to create your
own stringifier inside the class definition.

If you want to have the string "Judy\n" in your object, pass it as a
single string : $judy = new Person ("Judy\n");

--
Schraalhans Keukenmeester - sc*********@the .Spamtrapexampl e.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples ','oranges') < 0"

Jun 11 '07 #2
On 11.06.2007 06:46 phpCodeHead wrote:
Code which should allow my constructor to accept arguments:

<?php
class Person {
function __construct($na me)
{
$this->name = $name;
}

function getName()
{
return $this->name;
}

function printName()
{
print $this->name;
}

private $name;
}

$judy = new Person("Judy") . "\n"; // <- this is line parser don't
like
$joe = new Person("Joe") . "\n";

$judy->printName() . '<br />';
$joe->printName() . '<br />';
?>

Outputs:

Catchable fatal error: Object of class Person could not be converted
to string

Anyone seen this before? Know why? Am I missing something here?

Thanks all...

Gene Kelley
LAMP Web Developer
bizFlowDesigns. com
This is a new "feature" of 5.2 - for some mystical reason they've
removed implicit object-to-string conversion. You must provide explicit
__toString if you're printing or concatenating your objects.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 11 '07 #3
At Mon, 11 Jun 2007 14:46:45 +0200, gosha bine let h(is|er) monkeys type:
On 11.06.2007 06:46 phpCodeHead wrote:
>Code which should allow my constructor to accept arguments:

<?php
class Person {
function __construct($na me)
{
$this->name = $name;
}

function getName()
{
return $this->name;
}

function printName()
{
print $this->name;
}

private $name;
}

$judy = new Person("Judy") . "\n"; // <- this is line parser don't
like
$joe = new Person("Joe") . "\n";

$judy->printName() . '<br />';
$joe->printName() . '<br />';
?>

Outputs:

Catchable fatal error: Object of class Person could not be converted
to string

Anyone seen this before? Know why? Am I missing something here?

Thanks all...

Gene Kelley
LAMP Web Developer
bizFlowDesigns .com

This is a new "feature" of 5.2 - for some mystical reason they've
removed implicit object-to-string conversion. You must provide explicit
__toString if you're printing or concatenating your objects.
What should the contents of $judy be after assigning

$judy = new Person('Judy'). '\n';

be in your opinion?
'Judy\n' ?
'Object\n' ?
'sprinkled icecream\n' ?
'3.14159265\n' ?

Imho implicit obj2str conversion is meaningless, and though I have abused
the construct myself a few times I am glad they corrected this behaviour.
Having to define your own stringifier forces you to think about and
implement what is logically the proper meaning of a conversion to an
otherwise incompatible type.

I am not sure how many scripts out in the field rely on such implicit
conversion but I do sympathize with you; it's frustrating at times to find
yet another changed property between point releases. But don't you agree
it's still better to have to change some code to something more correct
than to have to deal with all kinds of odd behaviour?

Sh.

--
Schraalhans Keukenmeester - sc*********@the .Spamtrapexampl e.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples ','oranges') < 0"

Jun 11 '07 #4
On 11.06.2007 20:58 Schraalhans Keukenmeester wrote:
>
Imho implicit obj2str conversion is meaningless, and though I have abused
the construct myself a few times I am glad they corrected this behaviour.
Having to define your own stringifier forces you to think about and
implement what is logically the proper meaning of a conversion to an
otherwise incompatible type.
That's exactly the point Zend developers are constantly missing. It is
not the responsibility of language designers to remove constructs that
may seem "useless" to them. An application programmer is the one who
decides which syntax is appropriate for her particular task. The job of
the language designer is to provide clean and consistent mechanism for
generating any possible expression, including "useless" ones. You don't
let a taxi driver decide where you're going to go, do you?

As to this specific case, removal of implicit toString is especially
stupid, because _every other_ type in php and _every other_ comparable
programming language supports it.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 12 '07 #5
gosha bine wrote:
On 11.06.2007 20:58 Schraalhans Keukenmeester wrote:
>>
Imho implicit obj2str conversion is meaningless, and though I have abused
the construct myself a few times I am glad they corrected this behaviour.
Having to define your own stringifier forces you to think about and
implement what is logically the proper meaning of a conversion to an
otherwise incompatible type.

That's exactly the point Zend developers are constantly missing. It is
not the responsibility of language designers to remove constructs that
may seem "useless" to them. An application programmer is the one who
decides which syntax is appropriate for her particular task. The job of
the language designer is to provide clean and consistent mechanism for
generating any possible expression, including "useless" ones. You don't
let a taxi driver decide where you're going to go, do you?
No, but you let a taxi driver decide how you get there.
As to this specific case, removal of implicit toString is especially
stupid, because _every other_ type in php and _every other_ comparable
programming language supports it.
No, every other type in php doesn't support it. When was the last time
you tried to convert an object to an int, for instance?

And as for every other comparable programming language - no they don't.
But who cares? Other languages have features PHP doesn't have, and
PHP has features they don't have. It's comparing apples and oranges.

Personally, I've found the silent conversion allows for sloppy
programming and problems (like the one which started this thread). I'm
glad to see the implicit conversion is gone.

But if you really want it, the developers left a way for you to do so.
Good for them.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jun 12 '07 #6
On 12.06.2007 10:49 Jerry Stuckle wrote:
gosha bine wrote:
>On 11.06.2007 20:58 Schraalhans Keukenmeester wrote:
>>>
Imho implicit obj2str conversion is meaningless, and though I have
abused
the construct myself a few times I am glad they corrected this
behaviour.
Having to define your own stringifier forces you to think about and
implement what is logically the proper meaning of a conversion to an
otherwise incompatible type.

That's exactly the point Zend developers are constantly missing. It is
not the responsibility of language designers to remove constructs that
may seem "useless" to them. An application programmer is the one who
decides which syntax is appropriate for her particular task. The job
of the language designer is to provide clean and consistent mechanism
for generating any possible expression, including "useless" ones. You
don't let a taxi driver decide where you're going to go, do you?

No, but you let a taxi driver decide how you get there.
You are not trying to refute a _metaphor_, are you? ;)
>
>As to this specific case, removal of implicit toString is especially
stupid, because _every other_ type in php and _every other_ comparable
programming language supports it.

No, every other type in php doesn't support it. When was the last time
you tried to convert an object to an int, for instance?
I meant, every other php type supports implicit toString:

echo 123; - works
echo array(1, 2, 3); - works
echo fopen('blah', 'r'); - works

echo new Blah(); - DOES NOT work
>
And as for every other comparable programming language - no they don't.
Most languages support implicit toString for objects:

alert(new Blah()) - works in javascript
puts Blah.new - works in ruby
print Blah() - works in python
System.out.prin tln(new Blah()) - works in java

echo new Blah() - DOES NOT work in php

Are you saying other languages' designers care less about their
programmers? Why do they allow such a "useless" thing?
But who cares? Other languages have features PHP doesn't have, and PHP
has features they don't have. It's comparing apples and oranges.
Comparing programming languages and technologies is always productive,
there's always something to learn from each other.
>
Personally, I've found the silent conversion allows for sloppy
programming and problems (like the one which started this thread). I'm
glad to see the implicit conversion is gone.
What exactly are the problems which are solved by removing the silent
conversion?
>
But if you really want it, the developers left a way for you to do so.
Good for them.
An object without toString was converted to "object #x" or similar when
being printed. So was the behaviour prior to 5.2. Useless or not, it
simply worked. Now, it's broken and there's no way to make it work again.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 12 '07 #7
At Tue, 12 Jun 2007 10:22:23 +0200, gosha bine let h(is|er) monkeys type:
On 11.06.2007 20:58 Schraalhans Keukenmeester wrote:
>>
Imho implicit obj2str conversion is meaningless, and though I have abused
the construct myself a few times I am glad they corrected this behaviour.
Having to define your own stringifier forces you to think about and
implement what is logically the proper meaning of a conversion to an
otherwise incompatible type.

That's exactly the point Zend developers are constantly missing. It is
not the responsibility of language designers to remove constructs that
may seem "useless" to them.
It's not about being useless, it's about being meaningless without a
specific definition of what conversion should be like. Any construction
conceivable, logically sound or not, will be deemed useful by at least
some people, at some point.

The default string conversion hitherto available in PHP comes across as a
stopgap solution offering programmers a coincidental and semantically
debatable shortcut. There are valid tools available allowing you to find
out about anything you want to know about an object or its class, there is
no need for the implicit tostring conversion.
An application programmer is the one who
decides which syntax is appropriate for her particular task. The job of
the language designer is to provide clean and consistent mechanism for
generating any possible expression, including "useless" ones. You don't
let a taxi driver decide where you're going to go, do you?
I don't tell a taxi driver how to get at the destination best, how to
drive, when to shift gear and what his car should look like. I use a cab
because it's -based on sound empirical evidence- a well-defined service
using the tools and tricks required for the trade. I can expect a car with
a driver who knows how to operate a vehicle, has a fair knowledge of how
to get somewhere fastest or most efficiently. If there's a change in how
taxis operate, I'd hope that change isn't based on customer whim.
As to this specific case, removal of implicit toString is especially
stupid, because _every other_ type in php and _every other_ comparable
programming language supports it.
PHP is as PHP does. There's plenty of constructs PHP has that other
languages miss, and each has its own merits. If all languages only mimic
others we'll end up with the first generation T-ford colour chart. You can
choose any colour, as long as it's black.

At time I'd wish I could use list constructs and logic Prolog style, yet
I'd frown if Zend PHP decided to conform PHP to Prolog's way of doing
things.

There are traits in C I don't particularly like, but few if any of them
fail to make sense in logical terms. And yes I am glad C's more or less
calmed down and set in stone by now (start the flames people ;-)), and PHP
will come to that level someday, or not. Right now it's a highly dynamical
and slightly volatile language, and each cycle the true shape of PHP
becomes clearer. Access baggage is cut off, faults corrected and omissions
plugged.

But that's all just my opinion of course.
--
Schraalhans Keukenmeester - sc*********@the .Spamtrapexampl e.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples ','oranges') < 0"

Jun 12 '07 #8
gosha bine wrote:
On 12.06.2007 10:49 Jerry Stuckle wrote:
>gosha bine wrote:
>>On 11.06.2007 20:58 Schraalhans Keukenmeester wrote:

Imho implicit obj2str conversion is meaningless, and though I have
abused
the construct myself a few times I am glad they corrected this
behaviour.
Having to define your own stringifier forces you to think about and
implement what is logically the proper meaning of a conversion to an
otherwise incompatible type.

That's exactly the point Zend developers are constantly missing. It
is not the responsibility of language designers to remove constructs
that may seem "useless" to them. An application programmer is the one
who decides which syntax is appropriate for her particular task. The
job of the language designer is to provide clean and consistent
mechanism for generating any possible expression, including "useless"
ones. You don't let a taxi driver decide where you're going to go, do
you?

No, but you let a taxi driver decide how you get there.

You are not trying to refute a _metaphor_, are you? ;)
Sure! :-)
>>
>>As to this specific case, removal of implicit toString is especially
stupid, because _every other_ type in php and _every other_
comparable programming language supports it.

No, every other type in php doesn't support it. When was the last
time you tried to convert an object to an int, for instance?

I meant, every other php type supports implicit toString:

echo 123; - works
echo array(1, 2, 3); - works
echo fopen('blah', 'r'); - works

echo new Blah(); - DOES NOT work
Sure, and every other type is built in.
>>
And as for every other comparable programming language - no they don't.

Most languages support implicit toString for objects:

alert(new Blah()) - works in javascript
puts Blah.new - works in ruby
print Blah() - works in python
System.out.prin tln(new Blah()) - works in java

echo new Blah() - DOES NOT work in php
Well, for instance, C++ doesn't support it. And I've found Java's
default tostring() function to be particularly useless. That's why I
overload it in every class where I'm going to need it.
Are you saying other languages' designers care less about their
programmers? Why do they allow such a "useless" thing?
I'm not saying anything about any other languages' designers. All I'm
saying is I find it particularly useless.
> But who cares? Other languages have features PHP doesn't have, and
PHP has features they don't have. It's comparing apples and oranges.

Comparing programming languages and technologies is always productive,
there's always something to learn from each other.
Sure. But different languages have different syntax and different
features. That's why they're different languages. No one language is
good for everything.
>>
Personally, I've found the silent conversion allows for sloppy
programming and problems (like the one which started this thread).
I'm glad to see the implicit conversion is gone.

What exactly are the problems which are solved by removing the silent
conversion?
It makes you think about the implementation of the _tostring() method -
and specifically stops programming errors like which started this thread.
>>
But if you really want it, the developers left a way for you to do so.
Good for them.

An object without toString was converted to "object #x" or similar when
being printed. So was the behaviour prior to 5.2. Useless or not, it
simply worked. Now, it's broken and there's no way to make it work again.

Yea, it was broken before. "object #x" is real descriptive, isn't it?
And of course, you still have var_dump() and print_r() to help you,
don't you?

I just see absolutely no problem with removing the default. I would
have more of a problem if they didn't allow the programmer a means to
implement it, however.

But heck - I seldom used it. Last time I can think of using it was for
other than debugging was in a simple Point class, where it would print
"(x,y)" or "(r, theta)", depending on whether I was using Cartesian or
Polar coordinates.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jun 12 '07 #9
On 12.06.2007 17:06 Jerry Stuckle wrote:
>I meant, every other php type supports implicit toString:

echo 123; - works
echo array(1, 2, 3); - works
echo fopen('blah', 'r'); - works

echo new Blah(); - DOES NOT work

Sure, and every other type is built in.
Don't confuse 'type' and 'class'. 'Blah' is a class, 'object' is a
built-in type. 'echo some_resource' doesn't make more sense than 'echo
some_object', however it remains legal.

>>>
And as for every other comparable programming language - no they don't.

Most languages support implicit toString for objects:

alert(new Blah()) - works in javascript
puts Blah.new - works in ruby
print Blah() - works in python
System.out.pri ntln(new Blah()) - works in java

echo new Blah() - DOES NOT work in php

Well, for instance, C++ doesn't support it.
#include <iostream>

class A {};

int main() {
A* a = new A();
std::cout << a;
}

Works for me.

And I've found Java's
default tostring() function to be particularly useless. That's why I
overload it in every class where I'm going to need it.
Again, there's a difference between 'useless' and 'forbidden'. There are
many examples of completely useless code, do you think it all should be
forbidden at the language level?
>
>Are you saying other languages' designers care less about their
programmers? Why do they allow such a "useless" thing?

I'm not saying anything about any other languages' designers. All I'm
saying is I find it particularly useless.
And I wasn't talking about implicit toString being useful or not. I'm
just looking for the reason why it was taken out of the language.
>
>> But who cares? Other languages have features PHP doesn't have, and
PHP has features they don't have. It's comparing apples and oranges.

Comparing programming languages and technologies is always productive,
there's always something to learn from each other.

Sure. But different languages have different syntax and different
features. That's why they're different languages. No one language is
good for everything.
An interesting thought. Can I quote you on that? ;)
>
>>>
Personally, I've found the silent conversion allows for sloppy
programming and problems (like the one which started this thread).
I'm glad to see the implicit conversion is gone.

What exactly are the problems which are solved by removing the silent
conversion?

It makes you think about the implementation of the _tostring() method -
and specifically stops programming errors like which started this thread.
Thank you, I don't need a language feature that "makes me think". I
prefer to spend my time thinking about my problem domain, about my
algorithms and code structure and I'm not interested in "building
scaffolding to support the language itself" (c) Dave Thomas.
>>>
But if you really want it, the developers left a way for you to do
so. Good for them.

An object without toString was converted to "object #x" or similar
when being printed. So was the behaviour prior to 5.2. Useless or not,
it simply worked. Now, it's broken and there's no way to make it work
again.


Yea, it was broken before. "object #x" is real descriptive, isn't it?
And why didn't they make it more descriptive and useful?

And of course, you still have var_dump() and print_r() to help you,
don't you?

I just see absolutely no problem with removing the default.
I wouldn't call it a problem. It's just a small design bug, that breaks
some (not much) old code and makes php a bit more unpredictable and
inconsistent than before.
I would
have more of a problem if they didn't allow the programmer a means to
implement it, however.

But heck - I seldom used it. Last time I can think of using it was for
other than debugging was in a simple Point class, where it would print
"(x,y)" or "(r, theta)", depending on whether I was using Cartesian or
Polar coordinates.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
18825
by: Amy L. | last post by:
I have a class that contains a string array. However, I can't get this object to serialize in the xml output. Is there a trick to get a string to serialize? Thanks Amy.
4
1701
by: Earl Teigrob | last post by:
In the exampe below, the constructor of the container class is passed the string called "foo". Within the container class, I would like to create an instance(s) of foo and add them to the ArrayList that is returned. (Once I figure this out I will use reflection on the instance to do other stuff) Anyway, How to I dyanamically create a new instance of a class from ClassName string? Thanks for your Help
6
1154
by: ucasesoftware | last post by:
If u have a class person, do u add a property adress, phoneNumber in this class or is it better to add for exemple a class home with in adress, phoneNumber, etc...
6
1436
by: Brett Romero | last post by:
I'd like to get the string name of a specific public class property. Say I have a class named Person with a public property named PersonId (Int32 type). I create a new person object and do p.PersonId.ToString(). This returns the default value of PersonId - zero. I want it to return the string "PersonId". Any suggestions? Thanks, Brett
4
3885
by: John Smith | last post by:
How can I allow someone to cast my C# class into System.String? Is it possible in C#? In C++ I used "operator" keyword to mark C++ member function.
5
1582
by: mikie | last post by:
somebody asked me a question: string is derived from object class, right? (i said yes) so how it could be object class declares method returning string? thanks for any explanation / link / whatever :) mikie
5
1945
by: TazaTek | last post by:
Hello, I've seen some vague references on how to do this a factory, but not in enough detail to create one, or even know if it's what I need. Essentially, I'll have one of about 5 classes that could be referenced as a string (from a file). I'm not 100% sure of the relationship between the classes, but I suspect that they will not be derived from a base class, but in this
2
6552
by: Bigi | last post by:
Hi, Please help, this has been driving me nuts for nearly 2 days now. This vb6 code works: Public oEng As New ebizEngine Public oMsg As ebizMessage Function EbizGetFromQueue() As String
1
2900
by: braineater | last post by:
Define a base class person that will contain universal information, including name, address, birth date, gender and identification (student, worker etc). Derive from this class the following classes: Student Worker Student_worker Write a program that asks user to input information (student, worker etc) and creates a list of persons. Give user choice to view list by gender and by identification( list of people who are students, list of...
0
9906
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11051
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...
1
10774
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10379
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
9528
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
7088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5757
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
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.