php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30421 __toString() wont work
Submitted: 2004-10-13 10:08 UTC Modified: 2004-10-17 13:22 UTC
From: melnikow at hotbox dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.1 OS: WinXP
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: melnikow at hotbox dot ru
New email:
PHP Version: OS:

 

 [2004-10-13 10:08 UTC] melnikow at hotbox dot ru
Description:
------------
The new __toString() magic method wont work

Reproduce code:
---------------
The new __toString() magic method allows you to overload the object to string conversion:
<?php 
class Foo { 
   function __toString() { 
       return "What ever"; 
   } 
} 

$obj = Foo; 
$str = (string) $obj; // call __toString() 
echo $obj; // call __toString() 
?> 

Expected result:
----------------
What ever

Actual result:
--------------
Object#n

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-13 10:17 UTC] derick@php.net
Your script is bogus:
Notice: Use of undefined constant Foo - assumed 'Foo' in - on line 8


 [2004-10-13 18:22 UTC] melnikow at hotbox dot ru
Sorry, assumes that

$obj = new Foo();
 [2004-10-14 07:58 UTC] derick@php.net
Still works as expected:

derick@kossu:~$ php-5.0dev bug30421.php

What ever


derick@kossu:~$ php-5.0dev -v bug30421.php

PHP 5.0.2-dev (cli) (built: Sep 12 2004 11:50:47) (DEBUG)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.2-dev, Copyright (c) 1998-2004 Zend Technologies

 [2004-10-17 11:09 UTC] melnikow at hotbox dot ru
// A construction 
echo $obj; 
// realy works as expected and the output is a string: What ever

// But using this object in string context or explicit
// type casting (string) return unexpected result:
echo "Any string $obj";
// Output: Any string Object id #1
 [2004-10-17 13:10 UTC] derick@php.net
Yes, this is expected behavior. No bug here.
 [2004-10-17 13:19 UTC] melnikow at hotbox dot ru
ok
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC