Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111506 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57437 invoked from network); 13 Aug 2020 15:35:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Aug 2020 15:35:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C472A1804F2 for ; Thu, 13 Aug 2020 07:35:37 -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=1.7 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from forward500p.mail.yandex.net (forward500p.mail.yandex.net [77.88.28.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 13 Aug 2020 07:35:36 -0700 (PDT) Received: from mxback24j.mail.yandex.net (mxback24j.mail.yandex.net [IPv6:2a02:6b8:0:1619::224]) by forward500p.mail.yandex.net (Yandex) with ESMTP id CCC63940342; Thu, 13 Aug 2020 17:35:34 +0300 (MSK) Received: from localhost (localhost [::1]) by mxback24j.mail.yandex.net (mxback/Yandex) with ESMTP id PvswIxdVOg-ZYnmdEcT; Thu, 13 Aug 2020 17:35:34 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=softsatsistemas.com.br; s=mail; t=1597329334; bh=4+H+NMkz9fOaVaOeXBxgZjiVrkWzNBmOTbS3qx/WigE=; h=Message-Id:Cc:Subject:In-Reply-To:Date:References:To:From; b=G9rveMDoDnqxgfcEmINTIcmpQpimNpTzPCQQMfoAu3kh/5gqTzAx2mkTWgTc/zAc1 f7iBTGyO7NkkFzEDRcxqndHk6G55TgBZhn0ibaHI8ucPFjHW95ZSYeI8IEOpGF9mfI LvDy1KULy2EjowzJEQAEc4gvaw4Y3jhGEGISDEec= Authentication-Results: mxback24j.mail.yandex.net; dkim=pass header.i=@softsatsistemas.com.br Received: by myt2-a7d7570d35ff.qloud-c.yandex.net with HTTP; Thu, 13 Aug 2020 17:35:33 +0300 To: Claude Pache Cc: "internals@lists.php.net" In-Reply-To: <98AE3057-732B-4F32-9A13-F5C535A90953@gmail.com> References: <641541597256204@mail.yandex.com> <1252211597259017@mail.yandex.com> <98AE3057-732B-4F32-9A13-F5C535A90953@gmail.com> MIME-Version: 1.0 X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Thu, 13 Aug 2020 11:35:33 -0300 Message-ID: <811671597329181@mail.yandex.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8 Subject: [PHP-DEV] Nested functions From: luis@softsatsistemas.com.br (Luis - SoftSAT Sistemas) 13.08.2020, 10:52, "Claude Pache" : >> Le 12 août 2020 à 22:05, Luis - SoftSAT Sistemas a écrit : >> >>>>  Le 12 août 2020 à 20:19, Luis - SoftSAT Sistemas a écrit : >>>> >>>>  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. >>>> >>>>  I am new here, sorry if it was already discussed, i can't find a good way to search the mailing history. >>> >>> Hi, >>> >>> Defining a global function inside another function is not useless. Here is a dummy example: >>> >>> ```php >>> // a.php >>> spl_autoload_register(function ($classname) { >>>     if ($classname === 'Foo\\B') >>>         require 'foo/b.php'; >>> }) >>> >>> // foo/b.php >>> namespace Foo; >>> >>> class B { >>>     // ... >>> } >>> >>> function some_helper(....$args) { >>>     // ... >>> } >>> ``` >>> >>> The global function `Foo\some_helper` is defined inside the function used as autoloader. >>> >>> Also, from the RFC: >>> >>>>  They exist in the global namespace. >>> >>> I think you meant “They exist in the global scope”? because they are defined in the namespace corresponding to the eventual `namespace` declaration of the file. >>> >>> —Claude >> >> You are right Claude, i wasn't thinking about this use case. >> >> 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); >> } >> >> foo(); >> >> var_dump(bar()); >> >> //b.php >> $something = 10; >> >> 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 = 10;` to `$GLOBALS['something'] = 10;`, but that doesn't change the fact that this behavior are odd and not intuitive. >> >> PS: I known that global variables are a bad thing, it was just an example. >> >> Anyway now i see that changing this will break the auto loader system. >> >> 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. >> >> --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 = 10; > > function bar() { >    global $something; >    return $something; > } > ``` > > Functions in PHP have the following characteristics: they are defined globally, and they don’t 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’t even listed on https://wiki.php.net/rfc > > —Claude I think it's all clear now, thanks Claude! -- L. Henrique