php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #28165 Missing implicit object to integer conversion
Submitted: 2004-04-26 19:10 UTC Modified: 2004-04-29 21:07 UTC
Votes:17
Avg. Score:4.8 ± 0.5
Reproduced:15 of 15 (100.0%)
Same Version:10 (66.7%)
Same OS:11 (73.3%)
From: daniel at rozsnyo dot com Assigned:
Status: Suspended Package: Feature/Change Request
PHP Version: 5.0.* OS: *
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: daniel at rozsnyo dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-26 20:39 UTC] helly@php.net
There are no plans for this. Maybe in PHP 6.
 [2004-04-26 21:11 UTC] rozsnyo at kn dot vutbr dot cz
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)
 [2004-04-29 20:53 UTC] hans at velum dot net
Using '===' instead of '==' will work, but is semantically different, right?

$a === $b : are object references the same? i.e. are $a and $b the same instance.

$a == $b : do the contents match? i.e. do all the variables in object $a have the same values as the vars in object $b.

If '==' is going to be changed to mean something else, then I think PHP5 needs an __equals() method so that object contents comparison can be accomplished.
 [2004-04-29 21:07 UTC] daniel at rozsnyo dot com
OK, I have a "temporary" solution (because i wanted to compare the object references):

Instead of this:
  echo (in_array($x,$a)) ? 'There' : 'NOT there';

I must use this:
  echo (in_array($x,$a,TRUE)) ? 'There' : 'NOT there';

This way the PHP uses the '===' operator which is OK for me.

ad the '==' operator)
The notice of "Notice: Object of class A could not be converted to integer in..." is a bit ambiguous - I spent a day searching how to make an integer from an object. Anyway, the '==' works in PHP, but generates these notices - what if I really want to compare the objects by value?
 [2005-12-16 17:52 UTC] php at jort dot net
I updated form 5.0.4 to 5.1.1 and encountered this problem.
I use == and try to compare a DOM node value with a class property.
What's the difference between these versions?
 [2007-05-10 16:26 UTC] john at pinesoft dot co dot uk
problem still evident in 5.2.1

<?php
$obj = new stdClass;
var_dump($obj == 1);
?>
 [2007-05-10 16:32 UTC] john at pinesoft dot co dot uk
correction to previous comment.. version 5.2.2
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC