Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111911 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75054 invoked from network); 21 Sep 2020 18:53:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Sep 2020 18:53:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 32B54180549 for ; Mon, 21 Sep 2020 11:02:48 -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, 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-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (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 ; Mon, 21 Sep 2020 11:02:47 -0700 (PDT) Received: by mail-ed1-f41.google.com with SMTP id b12so13652814edz.11 for ; Mon, 21 Sep 2020 11:02:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=gDSzYbAjCTCKofWY800XDJ0HKJpEyyMznej/FEqy1Y8=; b=nr6+YtJT2jqnV+JsN+Sy0ezbkHOQ21P1DgokVVJdBBXzpIx9mmzGYu6uZUkn9YJMyS gv9BRIASUfVhXxv5ghKcn2lqYCABG7BHe/pwLCklYCWpmpJBXP/hxw4by3Id3CjYCfpy d1Q0QKlJP6YGutI5s73Q5n4Ar/z2Vitd+0mvniLGI0z5JMU/xyeK7TJpntFWPKxa4T5K r1NDNlEK1G9GL8K8tVDlmrNMLNZRJpJAYsEOflgm0QOSxXba+3TrOBKsJLAHSA81aOwq L12uXBz3qDw/P2Mmsibaxe6cCiozvi5SuJoPS/9DhmOpBYMiWEkal3Fq2Go6Eie2CHds WnFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=gDSzYbAjCTCKofWY800XDJ0HKJpEyyMznej/FEqy1Y8=; b=mqVUONV7O0nkrVekAFwlFzU4PfVsqUayXg283x0Xj2rxKzfJ6GbIHe3hLlczNu/PQD 7VCGzNN9MsE4ZY4HibsSqYcZAkGT2SFL7AsVZJbxt7k5nGeIak4k5oEU+cu88HOT4Yvi cPc6ZQyGH95I/uhxQ0D6SrX48ZY7ERaGNI4Ro0ew9Te8VIatG88xiKubyWOUsHj0jbrZ Tr4EEEjfylfZm5jeWpFK0NrxInOExYj0eS2O3AI7P7vSPgZ6eaDT52dNtIoPNu5Kmx4d MJjb2GQ1RsNL122PmHjko3K1c7MZdO9lkA0Jcii2SUlK1UKqAQ4Da9AHdlgUylvIJWIi Yapw== X-Gm-Message-State: AOAM532gcOZ7sYOMTIPz5ejuJHhvqUaeLdc5u6kINbpmddYSAYaLWFoe BDEMeSimOHQBmtKq8x1euY1CO23yHUGQ6vfo0L8= X-Google-Smtp-Source: ABdhPJzIJuXktFPjCkcqMUFA80+3Zfozv6TLY/qLfTrBsksj4ND+8uifg++nRzQ6z25na1Jdjkg+BLP7cLU+XZmsAic= X-Received: by 2002:a50:8524:: with SMTP id 33mr117112edr.123.1600711363339; Mon, 21 Sep 2020 11:02:43 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a54:3ac6:0:0:0:0:0 with HTTP; Mon, 21 Sep 2020 11:02:42 -0700 (PDT) In-Reply-To: References: <994c6693-a405-ce0f-399c-1518a7eeb321@gmail.com> Date: Mon, 21 Sep 2020 18:02:42 +0000 Message-ID: To: Michael Morris Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Namespace-private class properties From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2020-09-21 16:13 GMT, Michael Morris : > This sort of thing would be useful. Drupal and Symfony both mark methods in > their libraries that aren't supposed to be used externally, but people do > anyway and then they get mad at the framework developers when they decide > to rearrange what are supposed to be internal methods. > > I wrote a userland assertion to enforce just this sort of thing - enforcing > Java's notion of protected on methods otherwise marked as public. It worked > by looking up the caller in the debug_backtrace and checking to see if the > namespaces matched. To keep the impact of this check minimal I used > assert() for the check so that it wouldn't bog down production (where, > presumably, no new code would be written). > > Now, the above worked, but you could hack in by lying about your namespace > in your file declaration. Since, at the end of the day, PHP's notion of > namespace is entirely a string replace with no other enforcement there's no > way I can see of changing this short of fundamentally changing how PHP > handles namespaces. But I wonder if that should be bothered with since > someone would have to go considerably out of their way to make a call to a > method they weren't supposed to, and they only have themselves to blame if > this sort of solution becomes unstable. > > Next thing to consider - we have the problem of having already used the > protected keyword in PHP, and honestly I prefer PHP's interpretation of the > word. However, PHP doesn't have class scope at this time, so one > possibility is to allow class scope to set a default scope of the unscoped > methods and members of the class. So... > > public class A { > function foo () {} // Callable from anywhere. > } > > protected class B { > function foo () {} // Callable only from current namespace, or any child > namespace. > public function moo() {} // Callable anywhere. In effect the method > specific scope overrides class scope. > } > > private class C { > function foo () {} // Callable only from the same namespace, no children > can access. This includes a extending child function from another namespace > protected function moo () {} // Callable from any child class regardless > of that child's namespace. > } > > That could work. Feel free to poke holes in this - I'm sure I've missed > something. This syntax is already used for the PR about namespace encapsulation. Also, I think visibility always should be explicit. Olle