Bug #78609 [Opn->Ver]: mb_check_encoding() doesn't support objects with __toString() method any more

From: Date: Sun, 29 Sep 2019 13:45:03 +0000
Subject: Bug #78609 [Opn->Ver]: mb_check_encoding() doesn't support objects with __toString() method any more
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222946@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78609&edit=1

 ID:                 78609
 Updated by:         cmb@php.net
 Reported by:        kurt at suvaline dot com
 Summary:            mb_check_encoding() doesn't support objects with
                     __toString() method any more
-Status:             Open
+Status:             Verified
 Type:               Bug
 Package:            mbstring related
 Operating System:   Linux
-PHP Version:        7.3.10
+PHP Version:        7.2
 Block user comment: N
 Private report:     N

 New Comment:

Confirmed: <https://3v4l.org/tRJm3>.


Previous Comments:
------------------------------------------------------------------------
[2019-09-29 13:19:06] kurt at suvaline dot com

Description:
------------
mb_check_encoding() takes either string or array as first parameter. It seems that with added
support for arrays we might have lost the ability to provide objects with __toString() methods as
input. I would expect that when object of a class having __toString() method is provided as input
for mb_check_encoding() then the result of __toString() will be used. Instead we currently get the
following warning: Warning: mb_check_encoding(): Input is something other than scalar or array in
/sandbox/mb-check-encoding-invalid-input.php on line 11

Possible related commit: https://github.com/php/php-src/commit/b651b967383082de0f9c15ad6fcb36dda4082b0d
Possible related part of code: https://github.com/php/php-src/blob/5d6e923d46a89fe9cd8fb6c3a6da675aa67197b4/ext/mbstring/mbstring.c#L4823-L4844

P.S. Also take note that documentation at https://www.php.net/manual/en/function.mb-check-encoding.php
does not mention array as being supported first argument type.

Test script:
---------------
<?php

class Foo
{
    public function __toString()
    {
        return 'string_representation';
    }
}

mb_check_encoding(new Foo, 'UTF-8');

Expected result:
----------------
Script runs in both PHP 7.1.32 and PHP 7.3.9 without warnings. When we remove __toString() method
then we get warnings in both PHP versions.

Actual result:
--------------
Script runs without warnings in PHP 7.1.32 but gives (unexpected) warning "Input is something
other than scalar or array" in PHP 7.3.9. When we remove __toString() method then PHP 7.1.32
gives warning "mb_check_encoding() expects parameter 1 to be string, object given" and PHP
7.3.9 gives warning "Input is something other than scalar or array"


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=78609&edit=1


Thread (7 messages)

« previous php.bugs (#222946) next »