Why?
Here is the demonstration:
array_merge(
new ArrayObject,
new ArrayObject,
);
Warning: array_merge(): Expected parameter 1 to be an array, object given
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>
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
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>
Il 25/07/21 19:03, Arno Welzel ha scritto:
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>
mmmmhhhhhhhhhh...
Just like MyClass is not a string... :|
alex:
Il 25/07/21 19:03, Arno Welzel ha scritto:
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>
mmmmhhhhhhhhhh...
Just like MyClass is not a string... :|
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>
Il 26/07/21 12:12, Arno Welzel ha scritto:
alex:
Il 25/07/21 19:03, Arno Welzel ha scritto:
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>
mmmmhhhhhhhhhh...
Just like MyClass is not a string... :|
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>
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)!!!
Il 26/07/21 12:12, Arno Welzel ha scritto:
alex:
Il 25/07/21 19:03, Arno Welzel ha scritto:
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>
mmmmhhhhhhhhhh...
Just like MyClass is not a string... :|
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>
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)!!!
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 379 |
Nodes: | 16 (2 / 14) |
Uptime: | 70:45:31 |
Calls: | 8,084 |
Calls today: | 2 |
Files: | 13,069 |
Messages: | 5,849,826 |