Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111505 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 52264 invoked from network); 13 Aug 2020 14:52:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Aug 2020 14:52:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 617411804D2 for ; Thu, 13 Aug 2020 06:52:43 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 13 Aug 2020 06:52:42 -0700 (PDT) Received: by mail-ed1-f53.google.com with SMTP id m20so4273375eds.2 for ; Thu, 13 Aug 2020 06:52:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=5NtJ6WbyJscSZohAKKQF0KBQrHHImSbbrr0womAmqHI=; b=MLzFTMPoXt592CE5EFniRUa7f+qfU1GjMrbGwymCz8qoOhKksrgtPEkYWhck4ogBH4 yzG/s8arXJorpZMXSqqT093GlQo2mavM/QHayoYY5P6OlXT7awjkVXMk2CyFrJ+JIUfz LKkO8XD3XIFwsFL4PWeVCN0yRBvFNsuZNuJHKiveLJro4675mCM//uSojRZdjusDMiky VAXr1atCGpxb7kS3t9KI1Ejrabo+y3zmg7BYa3NOKZUyzTORfbFA1V5Vf/87ZGI+KEuG HPdWNCp8m/gVsA9br3Ztx1Q3O+5+ScGLgYqaIFEhqKn4wi0WGbpeofmmP6RRNPpiT77D p5RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=5NtJ6WbyJscSZohAKKQF0KBQrHHImSbbrr0womAmqHI=; b=M16/9fyIbcnEBJ1kHUcSzk8k27wgcCUHj7JF91lGJjE2XfqK59+rQ8ZGoz9mIsnUly 1vmOarJ28TY8V1LdHGY6YDek0AvtvTy3R4qEgZcM658lNmpQHw+2vU3kxg1IPsrzxIF7 gzKyx0Xx6VJTITcRybU96s0mbLXtdaVRcqkwKK4eV7xoN9hMAMA+phfijKgAwmXcXZxx pFt5VzE15uxJFOXRFgZ7gtjH2jWHwGrkyMThdDB2IzjDTGfjrjwd2b0tr7bH5lKC4kuN yF5cewHznTfCtZ7chvb+S2MtheQfw2Ys5WOwEgwy+iHFmV2y45eLnueeCoS083CROIOD chRg== X-Gm-Message-State: AOAM533D+DjMD0gx0FQDMNtjOqvibLrR+5wtWiS0KQI5eGAhC9PUephn HT7ppcMMYUDZqQXFYowXG8RYJUYy X-Google-Smtp-Source: ABdhPJwz6svOPcrFuaYCfy+2MrUETPcEsRMEIAiYtrYlgXmvagOY5EfPZR5gAX49io8hQywOWn7fNw== X-Received: by 2002:a50:d1d4:: with SMTP id i20mr4899147edg.320.1597326760729; Thu, 13 Aug 2020 06:52:40 -0700 (PDT) Received: from claude.fritz.box ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id w18sm4211911ejk.72.2020.08.13.06.52.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 13 Aug 2020 06:52:40 -0700 (PDT) Message-ID: <98AE3057-732B-4F32-9A13-F5C535A90953@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_FA78A2AA-8A18-42F9-B33A-30269D2ABB58" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.1\)) Date: Thu, 13 Aug 2020 15:52:39 +0200 In-Reply-To: <1252211597259017@mail.yandex.com> Cc: "internals@lists.php.net" To: Luis - SoftSAT Sistemas References: <641541597256204@mail.yandex.com> <1252211597259017@mail.yandex.com> X-Mailer: Apple Mail (2.3608.120.23.2.1) Subject: Re: [PHP-DEV] Nested functions From: claude.pache@gmail.com (Claude Pache) --Apple-Mail=_FA78A2AA-8A18-42F9-B33A-30269D2ABB58 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Le 12 ao=C3=BBt 2020 =C3=A0 22:05, Luis - SoftSAT Sistemas = a =C3=A9crit : >=20 >>> Le 12 ao=C3=BBt 2020 =C3=A0 20:19, Luis - SoftSAT Sistemas = a =C3=A9crit : >>>=20 >>> In the effort to make PHP a better language i want to ask why not = "fix" nested functions as suggested in = (https://wiki.php.net/rfc/true-nested-function-support) or block their = usage? >>> The current behavior is a lot unexpected. >>>=20 >>> I am new here, sorry if it was already discussed, i can't find a = good way to search the mailing history. >>=20 >> Hi, >>=20 >> Defining a global function inside another function is not useless. = Here is a dummy example: >>=20 >> ```php >> // a.php >> spl_autoload_register(function ($classname) { >> if ($classname =3D=3D=3D 'Foo\\B') >> require 'foo/b.php'; >> }) >>=20 >> // foo/b.php >> namespace Foo; >>=20 >> class B { >> // ... >> } >>=20 >> function some_helper(....$args) { >> // ... >> } >> ``` >>=20 >> The global function `Foo\some_helper` is defined inside the function = used as autoloader. >>=20 >> Also, from the RFC: >>=20 >>> They exist in the global namespace. >>=20 >> I think you meant =E2=80=9CThey exist in the global scope=E2=80=9D? = because they are defined in the namespace corresponding to the eventual = `namespace` declaration of the file. >>=20 >> =E2=80=94Claude >=20 > You are right Claude, i wasn't thinking about this use case. >=20 > But outside of this use case more problems arise. > If `foo/b.php` have some global variable and some function that use = it the function breaks because the global variable will be "included" = into current scope, while the functions into namespace scope. > ```php > //a.php > function foo() { > require 'b.php'; > //var_dump($something); > } >=20 > foo(); >=20 > var_dump(bar()); >=20 > //b.php > $something =3D 10; >=20 > function bar() { > global $something; > return $something; > } > ``` > I expected the `var_dump(bar());` output to be `int (10)`, but it was = `NULL`, while if i do `var_dump($something);` from `foo()` it outputs = `int(10)` as "normally expected". > Given that "bar()" are at namespace scope, this can be "fixed" = changing `$something =3D 10;` to `$GLOBALS['something'] =3D 10;`, but = that doesn't change the fact that this behavior are odd and not = intuitive. >=20 > PS: I known that global variables are a bad thing, it was just an = example. >=20 > Anyway now i see that changing this will break the auto loader system. >=20 > About the RFC you are right, they are wrong about the scope, but the = RFC aren't mine, i just found it while searching about the subject. >=20 > --L. Henrique About global variables; it is an issue I encountered once upon a time = when refactoring code. The short term solution is to declare the global = variables both in the inner and the outer scope: ```php // b.php global $something; $something =3D 10; function bar() { global $something; return $something; } ``` Functions in PHP have the following characteristics: they are defined = globally, and they don=E2=80=99t inherit variables from their parent = scope. On the other hand, anonymous functions (closures) are available = locally, and they may import variables from their parent scope. Both = sort of functions have their usages. About the RFC page: It looks to me like an old abandoned stub. It = isn=E2=80=99t even listed on https://wiki.php.net/rfc = =20 =E2=80=94Claude= --Apple-Mail=_FA78A2AA-8A18-42F9-B33A-30269D2ABB58--