ID: 20681
Comment by: hughjonesd at yahoo dot co dot uk
Reported By: dparks at verinform dot com
Status: Closed
Bug Type: Feature/Change Request
Operating System: Win 2K SP3
PHP Version: 4.2.3
New Comment:

This same bug also applies to in_array (tested on PHP
version 4.2.0):

bash-2.05a$ php
<?
class Foo {
function ar() {echo "baz";}
}
$f = new Foo();
$a = array(1,2,3,$f);
echo in_array($f, $a);
?>

Warning: in_array(): Wrong datatype for first argument in
- on line 7


Previous Comments:
------------------------------------------------------------------------

[2002-12-21 10:02:04] [email]moriyoshiphp.net[/email]

I agree with you. Actually object oriented programming in PHP is
somewhat troublesome, as numerous bug reports on this language feature
shows.

BTW, I committed the patch for array_search() that enables to look up
objects in an array properly under ZendEngine2.

Thank you for the report.

closing...


------------------------------------------------------------------------

[2002-12-05 19:59:33] dparks at verinform dot com

Um, actually I was wrong about understanding. The code you posted
contains this line:

$this->value = val;

Which means that all comparisons come out to true. Two of my coworkers
missed this bug too, and so we all thought that comparing objects in
PHP always returned true.

Since objects seem to be compared in a more or less rational fashion,
it seems like array_search() and in_array() should be able to take
objects as the needle. I understand that PHP is only slowly moving
toward being OO (or something like it), but this should at least be
doented.

Also, it should at least be doented that objects cannot be used as
array indices.

------------------------------------------------------------------------

[2002-12-05 16:12:48] dparks at verinform dot com

Yes, I understand now. I will be very happy when Zend Engine 2 is
released!

------------------------------------------------------------------------

[2002-11-28 14:02:00] [email]moriyoshiphp.net[/email]

My apologies, I should have give more explanation on this.

The following script is nearly an equivalent of array_search().

<?php
function _array_search($needle, $haystack, $strict = false)
{
reset($haystack);
while ((list($key, $element) = each($haystack))) {
if ($strict) {
if ($element === $needle) {
return $key;
}
} else {
if ($element == $needle) {
return $key;
}
}
}
return false;
}
?>

This function actually accepts objects as a needle, but array_search()
doesn't. In this sense, array_search() should go along with objects as
you said.

However IMO it might be too kind to offer this functionality.

<?php
class foo {
var $value;

function foo($val) {
$this->value = val;
}
}

$a = new foo(1);
$b = new foo(2);
$c = $a;
$d = &$a;

var_dump($a == $b);
var_dump($a == $c);
var_dump($a == $d);
var_dump($c == $d);
var_dump($a === $b);
var_dump($a === $c);
var_dump($a === $d);
var_dump($c === $d);
var_dump($a == new foo(1));
var_dump($a == new foo(2));
var_dump($b === new foo(2));
?>

What do you think of the result? Some say this could be preferrable,
but others couldn't.

That's the reason I said the function doesn't need that feature. Isn't
this enough?


------------------------------------------------------------------------

[2002-11-27 14:31:45] dparks at verinform dot com

I don't understand what this has to do with how equality is handled by
the Zend engine. The doentation states that array_search can accept
"mixed" data in the first parameter, which would seem to imply that
objects should work. They don't. Therefore, array_search() is broken.
What am I missing?

Also, I have seen no mention in the PHP manual about how equality is
handled by the Zend engine. Should I file a doentation bug?

Thanks,

Daniel

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
[url]http://bugs.php.net/20681[/url]

--
Edit this bug report at [url]http://bugs.php.net/?id=20681&edit=1[/url]