Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58412 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41851 invoked from network); 1 Mar 2012 14:31:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Mar 2012 14:31:46 -0000 Authentication-Results: pb1.pair.com header.from=adamjonr@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=adamjonr@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.170 as permitted sender) X-PHP-List-Original-Sender: adamjonr@gmail.com X-Host-Fingerprint: 209.85.214.170 mail-tul01m020-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:43381] helo=mail-tul01m020-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DD/77-46815-1588F4F4 for ; Thu, 01 Mar 2012 09:31:46 -0500 Received: by obbwd1 with SMTP id wd1so809898obb.29 for ; Thu, 01 Mar 2012 06:31:43 -0800 (PST) Received-SPF: pass (google.com: domain of adamjonr@gmail.com designates 10.182.231.100 as permitted sender) client-ip=10.182.231.100; Authentication-Results: mr.google.com; spf=pass (google.com: domain of adamjonr@gmail.com designates 10.182.231.100 as permitted sender) smtp.mail=adamjonr@gmail.com; dkim=pass header.i=adamjonr@gmail.com Received: from mr.google.com ([10.182.231.100]) by 10.182.231.100 with SMTP id tf4mr1959068obc.56.1330612303128 (num_hops = 1); Thu, 01 Mar 2012 06:31:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=uj0ZRdrqQeYPIxoRocOVj4z2Nd6hN7skGKCLRS/9r40=; b=OX4pueeRc+zr/WBB2T/LBBkEmPgKo6P9yQyzPz8QwGlWVCq7+bwlLl6Locrub8QhVh ptu2kFDSnb6dPbcLZDZf6XGTu/SOh3koQHqXiQvTe/l+NSEU5AHxfO6ARi44zp67W4i1 Si9w4lEN9XrmMIcpKhkVxkNOja2Zjk2OCT9Eo= MIME-Version: 1.0 Received: by 10.182.231.100 with SMTP id tf4mr1713679obc.56.1330612303058; Thu, 01 Mar 2012 06:31:43 -0800 (PST) Received: by 10.182.19.104 with HTTP; Thu, 1 Mar 2012 06:31:43 -0800 (PST) In-Reply-To: References: Date: Thu, 1 Mar 2012 09:31:43 -0500 Message-ID: To: Lazare Inepologlou Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=f46d04479615a8552004ba2f513b Subject: Re: [PHP-DEV] Scalar Type Intentions From: adamjonr@gmail.com (Adam Jon Richardson) --f46d04479615a8552004ba2f513b Content-Type: text/plain; charset=ISO-8859-1 On Thu, Mar 1, 2012 at 8:33 AM, Lazare Inepologlou wrote: > Yes, I agree, the casting (or the failing to cast) has to happen on entry, > for the reasons that you have very well explained. > > However, I cannot understand what it means to cast an object to a scalar. > Does it always mean casting to string? Wouldn't that be slow in many cases? > Simple example: > I'm not sure I understand, so if I mischaracterize your concerns, please let me know. Of note, the scalar type hinting I've outlined does not automatically perform casts to any particular type of scalar. Rather, it would be the programmer's responsibility to perform the cast (as I performed in my example.) This way, only necessary, reasonable casts are performed, and information loss can be avoided. That said, in terms of performance, PHP's type juggling performs these types of casts all the time, so I don't think I'd be concerned. Any time we check for equality using ==, perform string concatenation with ints, etc., PHP's beautiful type juggling automatically performs these conversions for us without any effort on our part. I've never seen where this is the source of any performance issues in my profiling, but I must admit that I don't know the internals well enough to preclude this from ever being an issue. class A { > public $value = 1234; > public function __toString(){ return (string)$this->value; } > } > > function foo( int $x ) { // here "int" is used as an alias to "scalar" as > you suggest > return $x + 1; > } > > $a = new A; > foo( $a ); // casting $a to scalar upon calling, as it is possible after > all > > In this example, the integer value will have to be cast to a string only > to be cast back to integer (unless something else happens under the hoods > that I am not aware). > Speaking to your example, it would throw a catchable fatal error because the variable $a contains an object of type A and the function foo expects a scalar. The object would first have to be cast to a scalar. However, as you pointed out, currently objects can only implement the __toString() method (i.e., there's no __toInt, etc.), so one can't directly cast an object to an int. This seems contrived, though, because in the case of your example, if a function expects an integer, wouldn't you just call it with the appropriate object property: foo ($a->value); // works because the value property is a scalar (int) Thanks for your commentary :) Adam --f46d04479615a8552004ba2f513b--