php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37007 __tostring failure
Submitted: 2006-04-07 09:36 UTC Modified: 2006-04-07 09:42 UTC
From: spinalex_555 at hotmail dot com Assigned:
Status: Not a bug Package: Compile Failure
PHP Version: 5.1.2 OS: Win xp sp2
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: spinalex_555 at hotmail dot com
New email:
PHP Version: OS:

 

 [2006-04-07 09:36 UTC] spinalex_555 at hotmail dot com
Description:
------------
Using the magic function __tostring the result is an object and not a string.

Reproduce code:
---------------
class DataBaseConnector{

	protected $connection;
	private $host, $user, $pw, $db,$state = "unconnected";

	
	public function __construct($connection_data){
		$this->host = $connection_data[host];
		$this->user = $connection_data[user];
		$this->pw = $connection_data[pw];
		$this->db = $connection_data[db];
		if ($this->connect()) {
			$this->state = "connected";
		}
	}
	
	private function connect(){
		$this->connection = mysql_connect($this->host, $this->user, $this->pw);
		if (!$this->connection) return false;
		if (!mysql_select_db($this->db, $this->connection)) return false;
		return true;
	}
	...
	public function __tostring(){
		return $this->state;
	}
}


Expected result:
----------------
a string with "connected" or "unconnected"

Actual result:
--------------
new $x = DataBaseConnector(my_data)

echo $x; //it seems to be a string like "connected" or so
but
echo $x."<br>; //it returns Object id#1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-07 09:42 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 13:01:30 2024 UTC