Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ArrayObject class unusable as array

48 views
Skip to first unread message

alex

unread,
Jul 23, 2021, 4:18:00 AM7/23/21
to
Why?

Here is the demonstration:

array_merge(
new ArrayObject,
new ArrayObject,
);


Warning: array_merge(): Expected parameter 1 to be an array, object given

Arno Welzel

unread,
Jul 23, 2021, 6:36:22 AM7/23/21
to
alex:
Yes - this is the expected result. That's why there is getArrayCopy():

<https://www.php.net/manual/en/arrayobject.getarraycopy.php>


--
Arno Welzel
https://arnowelzel.de

alex

unread,
Jul 24, 2021, 3:34:38 AM7/24/21
to
Il 23/07/21 12:36, Arno Welzel ha scritto:
> alex:
>
>> Why?
>>
>> Here is the demonstration:
>>
>> array_merge(
>> new ArrayObject,
>> new ArrayObject,
>> );
>>
>>
>> Warning: array_merge(): Expected parameter 1 to be an array, object given
>
> Yes - this is the expected result. That's why there is getArrayCopy():
>
> <https://www.php.net/manual/en/arrayobject.getarraycopy.php>
>
>

class MyClass {
function __toString(){
return __CLASS__;
}
}

function my_function(string $string){
echo "$string\n";
}

echo "Object to string implicit conversion: OK \n";
my_function(
new MyClass
);

echo "Object to array implicit conversion: ERROR \n";
array_merge(
new ArrayObject,
new ArrayObject,
);

Output:

Object to string implicit conversion: OK
MyClass
Object to array implicit conversion: ERROR

Warning: array_merge(): Expected parameter 1 to be an array, object
given in /home/rino/Scaricati/esperimenti/merge-ArrayObject.php

Why?

Arno Welzel

unread,
Jul 25, 2021, 1:04:03 PM7/25/21
to
alex:

[...]
> echo "Object to array implicit conversion: ERROR \n";
> array_merge(
> new ArrayObject,
> new ArrayObject,
> );
>
> Output:
>
> Object to string implicit conversion: OK
> MyClass
> Object to array implicit conversion: ERROR
>
> Warning: array_merge(): Expected parameter 1 to be an array, object
> given in /home/rino/Scaricati/esperimenti/merge-ArrayObject.php

Because ArrayObject() is not an array.

See <https://www.php.net/manual/en/class.arrayobject.php>

alex

unread,
Jul 26, 2021, 3:40:57 AM7/26/21
to
Il 25/07/21 19:03, Arno Welzel ha scritto:
mmmmhhhhhhhhhh...
Just like MyClass is not a string... :|

Arno Welzel

unread,
Jul 26, 2021, 6:12:55 AM7/26/21
to
alex:
Yes, but MyClass provides the method __toString() which allows it to be
used *like* a string. When asked for, MyClass will just return a string
representation of itself as the return value of its __toString() method.

However - there is no __toArray() method to rturn an array when needed.
That's the reason why ArrayObject provides getArrayCopy() to get a copy
of the array object as an array value.

Also see:

<https://www.php.net/manual/en/language.oop5.magic.php>
<https://www.php.net/manual/en/arrayobject.getarraycopy.php>

alex

unread,
Jul 26, 2021, 6:48:50 AM7/26/21
to
Il 26/07/21 12:12, Arno Welzel ha scritto:
However, the method should not be called

class C extends ArrayObject {
function getArrayCopy(){
echo 'CALLED';
}
}

array_merge(
(array) new C,
(array) new C,
);

No output (CALLED)!!!

Jerry Stuckle

unread,
Jul 26, 2021, 2:38:33 PM7/26/21
to
Because you never called the function!

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

Arno Welzel

unread,
Jul 30, 2021, 6:34:32 AM7/30/21
to
alex:
Yes, that is what I am talking about all the time!

You CAN NOT cast classes to arrays and you CAN NOT use classes like
arrays. You MUST call the method of the class which gives you the
content as array value:

class C extends ArrayObject {
function getArrayCopy(){
echo 'CALLED';
}
}

$a = new C();
$b = new C();

array_merge(
$a->getArrayCopy(),
$b->getArrayCopy()
)

I have explained all there is to say about now. Please take your time to
understand how classes and types work in PHP before asking again why
your idea does not work.
0 new messages