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


Groups > comp.lang.php > #18757

Re: ArrayObject class unusable as array

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Arno Welzel <usenet@arnowelzel.de>
Newsgroups comp.lang.php
Subject Re: ArrayObject class unusable as array
Date Fri, 30 Jul 2021 12:34:27 +0200
Lines 88
Message-ID <imi2tiF3hjrU1@mid.individual.net> (permalink)
References <sddtvf$11ug$1@gioia.aioe.org> <ilvkcuFls6iU1@mid.individual.net> <sdgfq4$nd3$1@gioia.aioe.org> <im5jrrFsqrkU1@mid.individual.net> <sdlou2$1j06$1@gioia.aioe.org> <im7g50F9uq6U1@mid.individual.net> <sdm3u7$110l$1@gioia.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=iso-8859-15
Content-Transfer-Encoding 7bit
X-Trace individual.net WQ3KZVVZWirH/ZOGHPP9mQCvVKoZ1yAWFJFswQL/2OKw5oV90A
Cancel-Lock sha1:w4JcpYBi71KXmah2Za3R61ZagHQ=
In-Reply-To <sdm3u7$110l$1@gioia.aioe.org>
Xref csiph.com comp.lang.php:18757

Show key headers only | View raw


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

Back to comp.lang.php | Previous | NextPrevious in thread | Find similar


Thread

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

csiph-web