# Internal casting of objects as array keys

- Thread: https://externals.io/message/10021
- Original archive: https://news-web.php.net/php.internals/10021
- Started by Aidan Lister on 2004-05-22T06:40:36+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #10021 — Aidan Lister — 2004-05-22T06:40:36+00:00

URL: https://externals.io/message/10021#10021

Hello devs,

I'd like to discuss the use of objects as array keys.

<?php
class idobject {
    private $_id;
    function __construct ($id) { $this->_id = $id; }
    function __tostring () {  return (int)$this->_id; }
}

$blah1 = new idobject(1);
$blah2 = new idobject(2);
$blah3 = new idobject(3);
$blah4 = new idobject(4);

$array = array (
 $blah1 => 'blah1',
 $blah2 => 'blah2',
 $blah3 => 'blah3',
 $blah4 => 'blah4',
 );

echo "<pre>";
var_dump($array);
echo "</pre>";
?>

In PHP4 this results in an error,
In PHP5 there is no error, the array is simply empty.

An array key can only be a string/int, thus, when someone attempts to use an
array as a key would it not make sense to cast the object?

This behaviour would then be consistant with resources.

Regards,
Aidan

---

## #10087 — Aidan Lister — 2004-05-25T13:03:15+00:00

URL: https://externals.io/message/10021#10087 · In reply to: #10021

I'm going to go ahead and post this as a bug, unless there is a reason noone
replied...

"Aidan Lister" <aidan@php.net> wrote in message
news:20040522064028.3397.qmail@pb1.pair.com...
> Hello devs,
>
> I'd like to discuss the use of objects as array keys.
>
> <?php
> class idobject {
>     private $_id;
>     function __construct ($id) { $this->_id = $id; }
>     function __tostring () {  return (int)$this->_id; }
> }
>
> $blah1 = new idobject(1);
> $blah2 = new idobject(2);
> $blah3 = new idobject(3);
> $blah4 = new idobject(4);
>
> $array = array (
>  $blah1 => 'blah1',
>  $blah2 => 'blah2',
>  $blah3 => 'blah3',
>  $blah4 => 'blah4',
>  );
>
> echo "<pre>";
> var_dump($array);
> echo "</pre>";
> ?>
>
> In PHP4 this results in an error,
> In PHP5 there is no error, the array is simply empty.
>
> An array key can only be a string/int, thus, when someone attempts to use
an
> array as a key would it not make sense to cast the object?
>
> This behaviour would then be consistant with resources.
>
> Regards,
> Aidan

---

## #10088 — Andi Gutmans — 2004-05-25T13:12:56+00:00

URL: https://externals.io/message/10021#10088 · In reply to: #10021

This code doesn't seem to have changed. Can you send me a 3-4 liner which 
errors out in PHP 4 and not in PHP 4?

Thanks,

Andi

At 11:03 PM 5/25/2004 +1000, Aidan Lister wrote:
>I'm going to go ahead and post this as a bug, unless there is a reason noone
>replied...
>
>"Aidan Lister" <aidan@php.net> wrote in message
>news:20040522064028.3397.qmail@pb1.pair.com...
> > […]
>an
> > […]

---

## #10089 — Aidan Lister — 2004-05-25T14:29:37+00:00

URL: https://externals.io/message/10021#10089 · In reply to: #10088

Hi Andi,

Okay, it's not as simple as I thought it was - and you're correct, the
behaviour has not changed for php4/5.

Objects in both PHP4 and PHP5 will throw an error if you use the method
$blah[$someobject] = 'val'; (first method)
Warning:  Illegal offset type

However, both do not throw an error if you use $blah = array($someobject =>
'val'); (second method)

When we compare this behaviour with resources, it gets interesting. A
resource is casted to an interger when used as an array key for the first
method, but not the second.

This raises a number of questions:
1) Should the behaviour of method 1, and method 2 be the same.
2) Should the behaviour of objects be the same as resources (in the context
of casting for use as an array key)

Below is a sample script which shows the differences

--------------------
<pre>
<?php
// Create a sample object
class id_obj { var $_id; function id_obj($id) { $this->_id = $id; } }
// Create two new objects
$id1 = new id_obj(1);
$id2 = new id_obj(2);
// Add them to an array with two different methods
$array = array(); $array[$id1] = 'sdf';
$array2 = array ($id1 => 'id1', $id2 => 'id2');
echo "objects: first method:\n";
var_dump($array);
echo "objects: second method:\n";
var_dump($array2);

// Create two resources
$fp1 = fsockopen("www.example.com", 80, $errno, $errstr, 30);
$fp2 = fsockopen("www.example.com", 80, $errno, $errstr, 30);
// Add them to an array with two different methods
$resarray = array(); $resarray[$fp1] = 'fp1';
$resarray2 = array($fp1 => 'fp1', $fp2 => 'fp2');
// Show the result
echo "resources: first method:\n";
var_dump($resarray);
echo "resources: second method:\n";
var_dump($resarray2);
?>
</pre>
--------------------

The output:

-----------
Warning:  Illegal offset type on line 9
objects: first method:
array(0) {
}
objects: second method:
array(0) {
}
resources: first method:
array(1) {
  [2]=>
  string(3) "fp1"
}
resources: second method:
array(0) {
}
--------------

Thanks.



"Andi Gutmans" <andi@zend.com> wrote in message
news:5.1.0.14.2.20040525161238.061c8f78@127.0.0.1...
> This code doesn't seem to have changed. Can you send me a 3-4 liner which
> errors out in PHP 4 and not in PHP 4?
>
> Thanks,
>
> Andi
>
> At 11:03 PM 5/25/2004 +1000, Aidan Lister wrote:
> > […]
noone
> > […]
use
> > […]

---

## #10170 — Aidan Lister — 2004-05-29T12:51:59+00:00

URL: https://externals.io/message/10021#10170 · In reply to: #10089

Andi, any interest in this?

"Aidan Lister" <aidan@php.net> wrote in message
news:20040525143059.95129.qmail@pb1.pair.com...
> Hi Andi,
>
> Okay, it's not as simple as I thought it was - and you're correct, the
> behaviour has not changed for php4/5.
>
> Objects in both PHP4 and PHP5 will throw an error if you use the method
> $blah[$someobject] = 'val'; (first method)
> Warning:  Illegal offset type
>
> However, both do not throw an error if you use $blah = array($someobject
=>
> 'val'); (second method)
>
> When we compare this behaviour with resources, it gets interesting. A
> resource is casted to an interger when used as an array key for the first
> method, but not the second.
>
> This raises a number of questions:
> 1) Should the behaviour of method 1, and method 2 be the same.
> 2) Should the behaviour of objects be the same as resources (in the
context
> of casting for use as an array key)
>
> Below is a sample script which shows the differences
>
> --------------------
> <pre>
> <?php
> // Create a sample object
> class id_obj { var $_id; function id_obj($id) { $this->_id = $id; } }
> // Create two new objects
> $id1 = new id_obj(1);
> $id2 = new id_obj(2);
> // Add them to an array with two different methods
> $array = array(); $array[$id1] = 'sdf';
> $array2 = array ($id1 => 'id1', $id2 => 'id2');
> echo "objects: first method:\n";
> var_dump($array);
> echo "objects: second method:\n";
> var_dump($array2);
>
> // Create two resources
> $fp1 = fsockopen("www.example.com", 80, $errno, $errstr, 30);
> $fp2 = fsockopen("www.example.com", 80, $errno, $errstr, 30);
> // Add them to an array with two different methods
> $resarray = array(); $resarray[$fp1] = 'fp1';
> $resarray2 = array($fp1 => 'fp1', $fp2 => 'fp2');
> // Show the result
> echo "resources: first method:\n";
> var_dump($resarray);
> echo "resources: second method:\n";
> var_dump($resarray2);
> ?>
> </pre>
> --------------------
>
> The output:
>
> -----------
> Warning:  Illegal offset type on line 9
> objects: first method:
> array(0) {
> }
> objects: second method:
> array(0) {
> }
> resources: first method:
> array(1) {
>   [2]=>
>   string(3) "fp1"
> }
> resources: second method:
> array(0) {
> }
> --------------
>
> Thanks.
>
>
>
> "Andi Gutmans" <andi@zend.com> wrote in message
> news:5.1.0.14.2.20040525161238.061c8f78@127.0.0.1...
> > […]
which
> > […]
> noone
> > […]
to
> use
> > […]
