Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66187 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 23984 invoked from network); 25 Feb 2013 14:00:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Feb 2013 14:00:23 -0000 Authentication-Results: pb1.pair.com header.from=krebs.seb@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=krebs.seb@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.53 as permitted sender) X-PHP-List-Original-Sender: krebs.seb@gmail.com X-Host-Fingerprint: 74.125.82.53 mail-wg0-f53.google.com Received: from [74.125.82.53] ([74.125.82.53:64063] helo=mail-wg0-f53.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 50/27-10787-57E6B215 for ; Mon, 25 Feb 2013 09:00:22 -0500 Received: by mail-wg0-f53.google.com with SMTP id fn15so2475590wgb.20 for ; Mon, 25 Feb 2013 06:00:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=xWbofW31KwenX17lJwqL+c4Iu00HUgKdDP3NE/kj5PY=; b=a4LPlLMF8/P0iRT2MFL1Bejxlj2OtvAxSO0mFn0mxRbST4usGa8UUpC3MsgeYouP2H hOte26nzP8nKU7N0mX5uDBzoX6p5FoSDPqxU8YW36M75Cfu2tYmH9uKmf28mO2ciDoyf PEcV7wXbhlERpbaTwwzW0F0/HXllLj3Gs1EAi0M6TE6D0B1nWTzWhd6LoxNtz/QuhpG0 6PgjRge2pnOnC56x5YNU5E8dKqU/bdQN6M+VXezlWqRK5YJgUkv+ZC2Uca37IILrNu1u YQJMGU7KamgJ1Irj7YSg20rp4el009Xg/W3s1VB7xZPUMnRmv2tCAlPtvwfZx6fFksrB BZfw== X-Received: by 10.180.80.35 with SMTP id o3mr12484415wix.9.1361800819138; Mon, 25 Feb 2013 06:00:19 -0800 (PST) MIME-Version: 1.0 Received: by 10.216.98.197 with HTTP; Mon, 25 Feb 2013 05:59:58 -0800 (PST) In-Reply-To: References: Date: Mon, 25 Feb 2013 14:59:58 +0100 Message-ID: To: nilsandre@gmx.de Cc: PHP internals list Content-Type: multipart/alternative; boundary=f46d044287f214609a04d68cf6b6 Subject: Re: [PHP-DEV] RFC Autoboxing Draft From: krebs.seb@gmail.com (Sebastian Krebs) --f46d044287f214609a04d68cf6b6 Content-Type: text/plain; charset=ISO-8859-1 2013/2/25 Nils Andre > Hi Everyone on the list, I have no RFC Karma here so far, so I post this to > the list at first. There has been ongoing discussion about new APIs and so > fort, so this is a suggestion for language cleanup by Autoboxing. I'd > really appreciate comments. > > == Introduction == > This RFC tries to approach the need for Autoboxing in PHP, or, to make > primitive types behave as if they were objects when being used or accessed > in an objectional fashion. Autoboxing can be discussed from various > perspectives, which is far beyond the scope of this document, which much > more addresses the fact that the language of PHP must be cleaned up in > order to grow more in terms of maturity and hence, in user accpetance. > > == Autoboxing in brief == > Autoboxing is that we can use primitive types, like integers, strings, > booleans and array as if they were objects, but without being constructed > as objects themselves. > > Autoboxing is widely discussed, and somehow a must-have in OOP as a > consequence to having primitive types (for the sake of performance and > memory overhead) but keeping the language OOP-consistent. > > == Why PHP needs it == > PHP needs autoboxing as a means to restructure the language and make it > more predictable. One big flaw of today's PHP is, that it is unstructured > and chaotic with its builtin functions, as, for example, those dealing with > strings. Not going too much into details here, many programmes always have > to look up the function definition because very similar functions have > their parameters in different orders or simply don't act in a predictive, > well-structured manner. This article is a good read to sum it up: > > http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/ > > The problem derives from the fact that PHP has not been designed top-down, > but has been developed the bottom-up way over many years: As of today, the > language offers features other languages don't have, but struggles with > it's buried bodies from the past (mainly because compatibility had and > still has to be kept.) > > By _adding autoboxing_ for all primitive types (Booleans, Strings, > Integers, Arrays), we would clean up the language in a very consistent > manner, but still retain backwards compatibility. > > == How it could be implemented == > We would not need much programming, because we simply map the new autoboxed > notation to the old code. We suggest the new notation to reproduce the Java > notation and parameter sequence closely, because it is pretty > straightforward and consistent (see, for example, the Javadocs on > java.lang.String). > > == Syntax == > The mapping is very straigtforward. Consider this: > > $a = "This is a string"; > > $b = $a->toUpperCase() --> $b = ucase($a) > $b = $a->substring(0,4) --> $b = substr($a, 0, 4) > > It would also allow many brilliant constructs like the following > > if ($b->startsWith("This")) { ... } > > in contrast to > > if (substr($b,0,4) == "This") { ... } > Must say, that for now I didn't read your mail completely, but this example is .. it doesn't fit. The problem is, that you compare two different semantics. function startsWith($string, $startsWith) { return !strncmp($string, $startsWith, strlen($startsWith)); } $b->startsWith('This'); startsWith($b, 'This'); Now they aren't that different anymore. > > Notice that the latter is error-prone, because if the condition changes one > would always have to match the string on the right handside to the length > of it on the left. > No: strlen($string) > > == Compatibility == > The old notation could be switched on/off at any time. > > The old notation would be switched on by default until, say, PHP 6, and can > then be enabled/disabled by either a compile flag, INI setting or some "use > \PHP5\*;" or something on top of a PHP file, which then makes the old > notation available to any code that follows/any file that is included > afterwards. As a consequence, \PHP5 will be the namespace comprising all > "old"/compatibility stuff. > > == Advantages == > - Cleanup of the language > - Consistency and predictability improved a lot (also in constrast to > possible userland classes - which would then all be slightly different) > - No lacking backwards compatibility > - Easier learning for new PHP programmers (beginners to PHP would be much > more pointed to learning OOP than procedural programming) > - Easier switching for programmers from Java to using PHP and vice versa > (now that PHP grew very mature, attract the Java folks with the cool stuff > PHP offers) > - Little overhead (as far as I am aware) to implement > - Nicer language syntax (see References) > > == Disadvantages == > - I don't know how complicated it is to implement. So far we don't have any > extensions for this on PECL, except for strong typing (see reference). From > a theoretical point of view, it is just mapping. > - There might be heaps of ways to implement, as the other autoboxing RFC > offers one possiblity. Certainly we need discussion on the best way to > implement. Maybe also "autoboxing" is the wrong word, as this RFC simply > suggests to map primitive types to some objectual syntax. > > == References == > This RFC [[https://wiki.php.net/rfc/autoboxing]] views it from the angle > of > nicer syntax. > > [[ > > http://php.webtutor.pl/en/2011/04/13/strong-data-typing-in-php-part-ii-autoboxing-and-indestructable-objects-english-version/ > ]] > implements a sort of userland "autoboxing" in PHP. > > [[http://www.php.net/manual/en/intro.spl-types.php]] is the documentation > of PECL SPL_Types, which make strongly-typed classes for primitive types > available. While they implement strong typing (if wanted), they do not > offer syntax cleanup. > -- github.com/KingCrunch --f46d044287f214609a04d68cf6b6--