|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-04-26 19:10 UTC] daniel at rozsnyo dot com
Description:
------------
While the docs says that i can use use objects as needle, i actually can not - it might be missing some implicit conversion to integer from objects.
(I use the published RC2 version (for win32), not the CVS version, the RC2 option is not available in the listbox for posting a bug-report)
Another problem is the notice (in RC1 there was no notice, here is a notice... so I am posting as bug... by the way, it would be nice to have functions like __toInteger() [in_array], __toArray() [for] besides the known __toString [echo,print]
Daniel
Reproduce code:
---------------
<?php
class A { }
$a = Array( new A, new A, new A );
echo $x = new A; // should be object # 4
echo (in_array($x,$a)) ? 'There' : 'NOT there';
?>
Expected result:
----------------
I am expecting this result:
---
Object id #4
NOT there
Actual result:
--------------
Look to notices:
---
Object id #4
Notice: Object of class A could not be converted to integer in C:\www\default\test\index.php on line 9
Notice: Object of class A could not be converted to integer in C:\www\default\test\index.php on line 9
There
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 17:00:01 2025 UTC |
The notices are also generated on comparison operator (==) This code: <?php class A { public $p; function __construct($ip) { $this->p = $ip; } } $x = new A(1); $y = $z = new A(2); echo '$x,$y is ' , ( ($x == $y) ? 'equal' : 'not equal[right]' ), '<br>'; echo '$x,$y is ' , ( ($x === $y) ? 'identical' : 'not identical[right]' ), '<br>'; echo '$z,$y is ' , ( ($z == $y) ? 'equal[right]' : 'not equal' ), '<br>'; echo '$z,$y is ' , ( ($z === $y) ? 'identical[right]' : 'not identical' ), '<br>'; ?> Produces: $x,$y is Notice: Object of class A could not be converted to integer in C:\www\default\test\index.php on line 11 Notice: Object of class A could not be converted to integer in C:\www\default\test\index.php on line 11 not equal[right] $x,$y is not identical[right] $z,$y is Notice: Object of class A could not be converted to integer in C:\www\default\test\index.php on line 13 Notice: Object of class A could not be converted to integer in C:\www\default\test\index.php on line 13 equal[right] $z,$y is identical[right] At least, please remove the notification and fix the in_array and also the array_search function (seems that both share the same algorithm - always two notices as two objects are compared) - the right solution would be to use the === operator in these functions when the needle is an object. This bug is still about: http://www.php.net/ChangeLog-5.php Changed array_search() to accept also objects as a needle. (Moriyoshi)