php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78609 mb_check_encoding() doesn't support objects with __toString() method any more
Submitted: 2019-09-29 13:19 UTC Modified: 2019-09-30 09:02 UTC
From: kurt at suvaline dot com Assigned: cmb (profile)
Status: Closed Package: mbstring related
PHP Version: 7.2 OS: Linux
Private report: No CVE-ID: None
 [2019-09-29 13:19 UTC] 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"

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-29 13:45 UTC] cmb@php.net
-Status: Open +Status: Verified -PHP Version: 7.3.10 +PHP Version: 7.2
 [2019-09-29 13:45 UTC] cmb@php.net
Confirmed: <https://3v4l.org/tRJm3>.
 [2019-09-30 07:45 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=348104
Log: Fix #78609: mb_check_encoding() no longer supports stringable objects
 [2019-09-30 07:50 UTC] salathe@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=463210b7775cc2c8e59c8139ae529c2085a33154
Log: Fix #78609: mb_check_encoding() no longer supports stringable objects
 [2019-09-30 07:50 UTC] salathe@php.net
-Status: Verified +Status: Closed
 [2019-09-30 08:11 UTC] nikic@php.net
-Status: Closed +Status: Re-Opened
 [2019-09-30 08:11 UTC] nikic@php.net
This still needs a php-src fix, right?
 [2019-09-30 09:02 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2019-09-30 09:02 UTC] cmb@php.net
Yes, this needs a php-src fix (the other commit was docs only).
I'm working on a PR.
 [2019-09-30 09:14 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #78609: mb_check_encoding() no longer supports stringable objects
On GitHub:  https://github.com/php/php-src/pull/4757
Patch:      https://github.com/php/php-src/pull/4757.patch
 [2019-09-30 11:08 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=45db6fa567b65e7ecd49b59527904fd8566ad813
Log: Fix #78609: mb_check_encoding() no longer supports stringable objects
 [2019-09-30 11:08 UTC] cmb@php.net
-Status: Re-Opened +Status: Closed
 [2020-02-07 06:04 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=7aea25a569723374ed703fbd01ec8783760cf7c1
Log: Fix #78609: mb_check_encoding() no longer supports stringable objects
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC