Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82443 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58371 invoked from network); 11 Feb 2015 07:41:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Feb 2015 07:41:56 -0000 Authentication-Results: pb1.pair.com smtp.mail=pajousek@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=pajousek@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.42 as permitted sender) X-PHP-List-Original-Sender: pajousek@gmail.com X-Host-Fingerprint: 209.85.192.42 mail-qg0-f42.google.com Received: from [209.85.192.42] ([209.85.192.42:38859] helo=mail-qg0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 61/0C-33902-2C70BD45 for ; Wed, 11 Feb 2015 02:41:54 -0500 Received: by mail-qg0-f42.google.com with SMTP id z107so1449429qgd.1 for ; Tue, 10 Feb 2015 23:41:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=843X0gcmmwFrsFhdTZ/GuzVZgK9y4B2ic0/ltPswsNg=; b=epBq3V5XTzGPRTxvY7QdrTmi4n20e+DtTF57WX5qaIe4UhtjnsvIlCkVa8jab6/nY8 k+FEVogpd3vZHzUqQRseN+pzlc1wMK0rxiB55gJW8xZe/+edM9yeWsi4Z2cds4Ss2l1X RCd9NWrFvpQguQoy6xotQc/mUmYiOfcAUz6yj6gM1XV6JC4lZ+WnmjPI0mIdmgAS2VR1 XMHbHXADXjr3UD6QuHsqb7Cl7k1GZ8hALl+4HCzpvPRa4+yk2V/KhZ4g9GzKsNTqWkCL EZrGjgt/G/HoNRDbotksAwPUtBPex25YXVPax+f9hcaFIE+PeU+6bLJ2531+ITS+rDoa vIGA== MIME-Version: 1.0 X-Received: by 10.224.30.17 with SMTP id s17mr3875629qac.77.1423640511924; Tue, 10 Feb 2015 23:41:51 -0800 (PST) Received: by 10.96.66.201 with HTTP; Tue, 10 Feb 2015 23:41:51 -0800 (PST) In-Reply-To: <54DAF884.7000508@lerdorf.com> References: <8703B53E-2C4A-4AC6-95C4-D4F19C6D5221@ajf.me> <54DAF884.7000508@lerdorf.com> Date: Wed, 11 Feb 2015 08:41:51 +0100 Message-ID: To: Rasmus Lerdorf Cc: Xinchen Hui , Andrea Faulds , PHP Internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [VOTE] Scalar Type Hints From: pajousek@gmail.com (=?UTF-8?Q?Pavel_Kou=C5=99il?=) On Wed, Feb 11, 2015 at 7:36 AM, Rasmus Lerdorf wrote: > On 02/10/2015 07:57 PM, Xinchen Hui wrote: >>> am I wrong?! >> seems I am wrong with this, it's a false alarm... it can restore automatically. > > Yeah, declare() doesn't span files so that isn't a problem. > > My worry is still the lack of type coercion for internal calls. I tested > it on some apps and it will take quite a bit of tedious and rather > useless effort to fix these to run in strict mode. Some examples of > common things that are fatal errors in strict mode: > > ini_set('precision', 14); > > ini_set('display_errors', 1); > > ini_set('display_errors', true); > > ok, not common, but tan(4/2) fatal, tan(5/2) no error > > Wordpress has this function, spot the error: > > function add_magic_quotes( $array ) { > foreach ( (array) $array as $k => $v ) { > if ( is_array( $v ) ) { > $array[$k] = add_magic_quotes( $v ); > } else { > $array[$k] = addslashes( $v ); > } > } > return $array; > } > > $v may not always be a string (it died with a float for me), so the > fix is to cast: > > $array[$k] = addslashes( (string)$v ); > > Also from Wordpress: > > $formatted = number_format( $number, absint( $decimals ), > $wp_locale->number_format['decimal_point'], > $wp_locale->number_format['thousands_sep'] ); > > Here number_format() is expecting a float but $number is a string. So > again, the only real fix is to cast. > > And in Drupal8 *without turning on strict*: > > use Drupal\Component\Utility\String; > > it dies with: "Fatal error: "string" cannot be used as a class name in > /var/www/drupal/core/includes/bootstrap.inc on line 11" > > That String class is everywhere in Drupal. They are going to have a > fun time with that. See > https://gist.githubusercontent.com/anonymous/d9252deeeb2aae1a5af5/raw/053155130d22551b1404d0a9b94e27424544b6d1/gistfile1 > > From Geeklog: > > if (strtolower($topic) != strtolower($archivetid)) { > $sql .= " AND ta.tid != '{$archivetid}' "; > } > > $topic can be null there. Looking at the logic, not really a bug, > just a natural reliance on null being coerced to "" > > Also from Geeklog: > > if( empty( $date )) { > // Date is empty, get current date/time > $stamp = time(); > } else if( is_numeric( $date )) { > // This is a timestamp > $stamp = $date; > } else { > // This is a string representation of a date/time > $stamp = strtotime( $date ); > } > // Format the date > $date = strftime( $dateformat, $stamp ); > > strftime() expects an integer for the timestamp there, but as the > above logic shows, they are expecting a numeric string to be fine. > No bug, just another forced cast. > > And another number_format() instance where arg1 is not necessarily > a float. Obviously not a bug, so we have to cast again: > > return number_format( $number, $dc, $ds, $ts ); > > In Opencart: > > $this->image = imagecreatetruecolor($width, $height); > imagecreatetruecolor() expects parameter 1 to be integer, string > given in /var/www/opencart/system/library/image.php on line 89 > > You could argue this is a bug, I guess, but the width and height are > coming from a database and are integers in the db, but since the db > returns strings. Another cast... > > I was genuinely hoping to find some bugs with this exercise. I suppose > it is because I did it on mature projects and at this stage those sorts > of bugs have already been fixed. But I still fear that people are going > to want to be enable strictness everywhere and then they will quickly > learn that they better cast stuff to be safe which makes the whole thing > rather pointless. And I feel pretty sorry for the Drupal folks. That > list of 1000+ instances of their String class is going to suck to fix. > > -Rasmus > Helo, the Drupal thing seems scary. Also, I realized one thing why I think the strict version is a bad idea for PHP (in the state PHP is now - in an ideal world I would love to have nothing but strongly typed PHP, but that's offtopic) - PHP has many functions that return multiple types, so you cannot do foo(bar()) if bar() has multiple return types. :( Regards Pavel Kouril