Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18729 > unrolled thread

ArrayObject class unusable as array

Started byalex <1j9448a02@lnx159sneakemail.com.invalid>
First post2021-07-23 10:17 +0200
Last post2021-07-30 12:34 +0200
Articles 9 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  ArrayObject class unusable as array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-23 10:17 +0200
    Re: ArrayObject class unusable as array Arno Welzel <usenet@arnowelzel.de> - 2021-07-23 12:36 +0200
      Re: ArrayObject class unusable as array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-24 09:34 +0200
        Re: ArrayObject class unusable as array Arno Welzel <usenet@arnowelzel.de> - 2021-07-25 19:03 +0200
          Re: ArrayObject class unusable as array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-26 09:40 +0200
            Re: ArrayObject class unusable as array Arno Welzel <usenet@arnowelzel.de> - 2021-07-26 12:12 +0200
              Re: ArrayObject class unusable as array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-26 12:48 +0200
                Re: ArrayObject class unusable as array Jerry Stuckle <jstucklex@attglobal.net> - 2021-07-26 14:38 -0400
                Re: ArrayObject class unusable as array Arno Welzel <usenet@arnowelzel.de> - 2021-07-30 12:34 +0200

#18729 — ArrayObject class unusable as array

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-23 10:17 +0200
SubjectArrayObject class unusable as array
Message-ID<sddtvf$11ug$1@gioia.aioe.org>
Why?

Here is the demonstration:

array_merge(
     new ArrayObject,
     new ArrayObject,
);


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

[toc] | [next] | [standalone]


#18731

FromArno Welzel <usenet@arnowelzel.de>
Date2021-07-23 12:36 +0200
Message-ID<ilvkcuFls6iU1@mid.individual.net>
In reply to#18729
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>


-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#18737

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-24 09:34 +0200
Message-ID<sdgfq4$nd3$1@gioia.aioe.org>
In reply to#18731
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?

[toc] | [prev] | [next] | [standalone]


#18749

FromArno Welzel <usenet@arnowelzel.de>
Date2021-07-25 19:03 +0200
Message-ID<im5jrrFsqrkU1@mid.individual.net>
In reply to#18737
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>

-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#18750

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-26 09:40 +0200
Message-ID<sdlou2$1j06$1@gioia.aioe.org>
In reply to#18749
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... :|

[toc] | [prev] | [next] | [standalone]


#18753

FromArno Welzel <usenet@arnowelzel.de>
Date2021-07-26 12:12 +0200
Message-ID<im7g50F9uq6U1@mid.individual.net>
In reply to#18750
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>

-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#18755

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-26 12:48 +0200
Message-ID<sdm3u7$110l$1@gioia.aioe.org>
In reply to#18753
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)!!!

[toc] | [prev] | [next] | [standalone]


#18756

FromJerry Stuckle <jstucklex@attglobal.net>
Date2021-07-26 14:38 -0400
Message-ID<sdmvf1$449$1@jstuckle.eternal-september.org>
In reply to#18755
On 7/26/2021 6:48 AM, alex wrote:
> 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)!!!

Because you never called the function!

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

[toc] | [prev] | [next] | [standalone]


#18757

FromArno Welzel <usenet@arnowelzel.de>
Date2021-07-30 12:34 +0200
Message-ID<imi2tiF3hjrU1@mid.individual.net>
In reply to#18755
alex:

> 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)!!!

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.

-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web