php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66237 Mutating __toString causes array_intersect to return empty
Submitted: 2013-12-04 21:53 UTC Modified: 2013-12-11 20:55 UTC
From: simon at simon dot geek dot nz Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.5Git-2013-12-04 (Git) OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: simon at simon dot geek dot nz
New email:
PHP Version: OS:

 

 [2013-12-04 21:53 UTC] simon at simon dot geek dot nz
Description:
------------
Hopefully the test script is self-explanatory. When running array_intersect on arrays that contains an object that mutates itself and the return value in its __toString() method, the result is an empty array even if there are literal values in the arrays that intersect.

Test script:
---------------
<?php

class A {
	protected $i = 0;

	public function __toString() {
		$this->i++;
		return "$this->i";
	}
}

$a = new A;

$b = ['100', $a];

var_dump(array_intersect($b, $b));


Expected result:
----------------
array(1) {
  [0]=>
  string(3) "100"
}


Actual result:
--------------
array(0) {
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-11 20:55 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2013-12-11 20:55 UTC] aharvey@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

array_intersect() is documented as comparing the string form of each element — if that's unstable, then bad things may happen.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 13 09:01:27 2025 UTC