php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52042 Error creating MessageFormatter object with nested tags
Submitted: 2010-06-11 01:52 UTC Modified: 2015-06-16 11:36 UTC
From: zabruska at tugamail dot pt Assigned: stas (profile)
Status: Closed Package: *Languages/Translation
PHP Version: 5.3.2 OS: Ubuntu 9.10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: zabruska at tugamail dot pt
New email:
PHP Version: OS:

 

 [2010-06-11 01:52 UTC] zabruska at tugamail dot pt
Description:
------------
Calling msgfmt_create(...) or new MessageFormatter(...) with patterns having nested tags will return null.

Apparently the same happens with empty patterns, but returning NULL from an object constructor is a SERIOUS bug.



Test script:
---------------
$f1 = new MessageFormatter('en_US', '{what is wrong {0} with this pattern ???}');
echo 'f1 is ', gettype($f1), PHP_EOL;

$f2 = MessageFormatter::create('en_US', '{what is wrong {0} with this pattern ???}');
echo 'f2 is ', gettype($f2), PHP_EOL;

$f3 = msgfmt_create('en_US', '{what is wrong {0} with this pattern ???}');
echo 'f3 is ', gettype($f3), PHP_EOL;

Expected result:
----------------
f1 is object
f2 is object
f3 is object

Actual result:
--------------
f1 is NULL
f2 is NULL
f3 is NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-11 01:56 UTC] zabruska at tugamail dot pt
Escaping the tags makes no difference either
 [2010-06-11 08:15 UTC] mike@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: stas
 [2010-06-11 08:15 UTC] mike@php.net
Constructors definitely should throw an Exception.
 [2010-06-15 23:53 UTC] stas@php.net
Use quotes to quote { inside the format string, since { is a special char for message formats.
Like this: 
$f1 = new MessageFormatter('en_US', '\'{\'what is wrong {0} with this pattern ???\'}\'');
 [2010-06-15 23:53 UTC] stas@php.net
-Status: Assigned +Status: Bogus
 [2010-06-15 23:53 UTC] stas@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2010-06-16 09:40 UTC] zabruska at tugamail dot pt
I can understand if msgfmt_create(...) or MessageFormatter::create(...) return NULL
on invalid patterns, but attempting to create any object using "new" should 
throw an exception.

Imagine this:

class MyMessageFormatter {
  function __construct($pattern) {
    if($this->badPattern($pattern)) {
      unset($this);
      return null;
    }
  }
  
  // ...
}

The above code makes no sense at all!!! PHP itself will compile the code, but will still 
give me an object.

I insist that returning NULL from a constructor is a BUG, sorry...
 [2015-06-16 11:36 UTC] danack@php.net
-Status: Not a bug +Status: Closed
 [2015-06-16 11:36 UTC] danack@php.net
This is fixed in PHP 7.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Mar 14 15:01:30 2025 UTC