Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111499 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53114 invoked from network); 12 Aug 2020 21:05:30 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Aug 2020 21:05:30 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 063181804AA for ; Wed, 12 Aug 2020 13:05:15 -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 forward501o.mail.yandex.net (forward501o.mail.yandex.net [37.140.190.203]) (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 ; Wed, 12 Aug 2020 13:05:14 -0700 (PDT) Received: from mxback18o.mail.yandex.net (mxback18o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::69]) by forward501o.mail.yandex.net (Yandex) with ESMTP id 2257C1E800E7; Wed, 12 Aug 2020 23:05:12 +0300 (MSK) Received: from localhost (localhost [::1]) by mxback18o.mail.yandex.net (mxback/Yandex) with ESMTP id 9asViNcNj4-5BYGIrfS; Wed, 12 Aug 2020 23:05:11 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=softsatsistemas.com.br; s=mail; t=1597262711; bh=Ye7uibRhd7Zjcs042BoepPbTpscNEZ01ekknhJqkatE=; h=Message-Id:Cc:Subject:In-Reply-To:Date:References:To:From; b=OdISqLcIIVjIxsJA3yM/RRfplsteUF7fvrzB00nm1s8YaF4YnEbP+bUelvXcncpln +CGo6jfKu49vQEaygTiBdgXdJBu/DhOjH48Wfdv5ybNuY24X/qshYHEdZowGkI9dqm thYACagG4TsI5x2R0OGwaG8wLHaV6RF3596tQxZs= Authentication-Results: mxback18o.mail.yandex.net; dkim=pass header.i=@softsatsistemas.com.br Received: by iva5-58d151f416d2.qloud-c.yandex.net with HTTP; Wed, 12 Aug 2020 23:05:11 +0300 To: Claude Pache Cc: "internals@lists.php.net" In-Reply-To: References: <641541597256204@mail.yandex.com> MIME-Version: 1.0 X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Wed, 12 Aug 2020 17:05:11 -0300 Message-ID: <1252211597259017@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) >>  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