Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #18388
| From | alex <1j9448a02@lnx159sneakemail.com.invalid> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: value returned instead change the value of the variable |
| Date | 2020-10-16 18:36 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <rmci6b$1lco$1@gioia.aioe.org> (permalink) |
| References | <rmbvqm$lnh$1@gioia.aioe.org> <rmcd7a$qh7$1@jstuckle.eternal-september.org> |
Il 16/10/20 17:11, Jerry Stuckle ha scritto:
> On 10/16/2020 7:23 AM, alex wrote:
>> // DESIGN 1
>> echo "It doesn't work (return 1 vs array), but I like 'DESIGN 1' ";
>> $arr = [1, 2];
>> print_r(
>> array_shift($arr)
>> );
>>
>> // DESIGN 2
>> echo "It works, but I don't like 'DESIGN 2': ";
>> $arr = [1, 2];
>> array_shift($arr);
>> print_r(
>> $arr
>> );
>>
>> Output:
>>
>> It doesn't work (return 1 vs array), but I like 'DESIGN 1': 1
>>
>> It works, but I don't like 'DESIGN 2': Array
>> (
>> [0] => 2
>> )
>>
>> To find a compromise I created this class:
>>
>> <?php
>> class FirstProcessedArgumentReturn {
>> static function __callStatic(string $name, array $arguments) {
>> $name($arguments);
>>
>> return $arguments[0];
>> }
>> }
>>
>> FirstProcessedArgumentReturn::array_shift([1, 2]);
>> //TODO: Test the method FirstProcessedArgumentReturn::array_push(...);
>> //TODO: Test the method FirstProcessedArgumentReturn::array_pop(...);
>> //TODO: Test the method ...
>>
>> PHP Notice: Undefined offset: 0 in /tmp/test.php on line 6
>>
>> But as you can see, something does not work: the reason I leave you to
>> imagine.
>>
>> Some idea?
>
> It's working fine - but you're doing two different things. array_shift()
> shifts the array elements one position left - AND RETURNS THE SHIFTED
> VALUE. So your first design is working properly.
>
> In your second design you are discarding the returned value and printing
> the array. So it, also, is working fine.
>
> Your static function is failing because you're not passing an array. You
> are passing the returned value from array_shift().
>
final class FirstProcessedArgumentReturn {
static function __callStatic(string $name, array $arguments) {
$name($arguments[0]);
return $arguments[0];
}
}
// OK
print_r(
FirstProcessedArgumentReturn::array_shift(
[1, 2]
)
);
// MISSING 3
print_r(
FirstProcessedArgumentReturn::array_push(
[1, 2],
3
)
);
Output:
Array
(
[0] => 2
)
Array
(
[0] => 1
[1] => 2
)
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-16 13:23 +0200
Re: value returned instead change the value of the variable Jerry Stuckle <jstucklex@attglobal.net> - 2020-10-16 11:11 -0400
Re: value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-16 18:36 +0200
Re: value returned instead change the value of the variable "J.O. Aho" <user@example.net> - 2020-10-16 20:51 +0200
Re: value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-17 09:26 +0200
Re: value returned instead change the value of the variable "J.O. Aho" <user@example.net> - 2020-10-17 10:16 +0200
csiph-web