Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111910 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 69399 invoked from network); 21 Sep 2020 17:04:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Sep 2020 17:04:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3470718053D for ; Mon, 21 Sep 2020 09:13: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-oi1-f195.google.com (mail-oi1-f195.google.com [209.85.167.195]) (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 09:13:39 -0700 (PDT) Received: by mail-oi1-f195.google.com with SMTP id 185so17539951oie.11 for ; Mon, 21 Sep 2020 09:13:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=3f8XcufFqwrH50qbhduFeOyRTr/bpPf5/IqUNsMHDpk=; b=E3CGUNvsSfXWiEWWg1BY5GB18U3E+c/CDzvqcfDa9hgOwWa2eVvAWif7t/tsmE8+6Q Sd84MlZJLt7oRsdWy8zoD+dkpstfirumM10T7/Vj69QUAqotPBBC1IfT4ldYCGeLZYnh 6OnBkg1FmpnJR79yYt70SAosLJ1RYoXje+YNDxhWXyl9M/JQxnzpPEr9osBnWtWInBX0 mG8KO36lIyzaT7ZeU2397x+rdU1OtTfgk3muxlfNy/kk/3pryKX3ec66UgVl0Z2JTAxg 9rvxH2VHqkqcfTAq/q++0j0vxsnDgilZuxnSsWBan5vUKj07ySWpoPuquxrF94Ytlv81 JKJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=3f8XcufFqwrH50qbhduFeOyRTr/bpPf5/IqUNsMHDpk=; b=HOMFcn6VhSCXZoGwEW44NCuIid3i0YRFd0mnFugA0Hq+MQFhsByc1+FrFJwO1IltQg n5ys2NEdjBVf3vjTULjnOFo49/ArB8ku/iMHHNCDTZl9k/G7mAMHD2E50Df+UUwRB7x7 aX7zn+2eDmiM244PTCeE/k82IoDxqnSxYpLj6p/Dp+GHr28YxW37P3K+CXdNqVc+LRcA JAVJykCtSoEK+G/LaFVyraOZX1Q0Db9x53GzFTBIZ6A6unBMkaBaodX0L/5xAvqY7Z4d 3z06NWWxxK7bNihCHxD82+Z/qK4aLMly5iK0ckuZRps440K9qFXC5GSHcZwfORgFS7ow CMrA== X-Gm-Message-State: AOAM533PBO/yheNJg456EYHoUhVZOAqyaEn3YPENCzFkGCp8A3g0eSsm Zj0DZiuobr+j+Vn8+hPRDJKOPHQK9UJulQNjdLaH25Q4vW4= X-Google-Smtp-Source: ABdhPJwoOBb8HXj2BG8+Uz313NmOXdx5BYsuRkB0lWPVfZomeXdQUsDC01UytGWMB1ZsdG4vtvUHyP50Ry9DChvxzvo= X-Received: by 2002:aca:b706:: with SMTP id h6mr90887oif.23.1600704816966; Mon, 21 Sep 2020 09:13:36 -0700 (PDT) MIME-Version: 1.0 References: <994c6693-a405-ce0f-399c-1518a7eeb321@gmail.com> In-Reply-To: <994c6693-a405-ce0f-399c-1518a7eeb321@gmail.com> Date: Mon, 21 Sep 2020 11:13:24 -0500 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="00000000000001720505afd522d7" Subject: Re: [PHP-DEV] Namespace-private class properties From: tendoaki@gmail.com (Michael Morris) --00000000000001720505afd522d7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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. On Sat, Sep 19, 2020 at 7:43 AM Rowan Tommins wrote: > On 17/09/2020 13:28, Olle H=C3=A4rstedt wrote: > > We have public, protected and private. Since PHP has no module system, = we > > have no qualifier to mark a class property as "private for this module"= . > > One solution to this could be to add a new qualifier "internal", to mak= e > > properties public within the current namespace. > > > As well as the implementation details of doing this at run-time (which > is where PHP does all its access checks right now) the mapping of > "namespace" to "module" is not necessarily the most useful one. > > For instance, if you define an "internal" property in a class called > Acme\Frobulator\Handlers\TextFile\Format\CSV, it would be accessible in > other classes in namespace Acme\Frobulator\Handlers\TextFile\Format\ and > possibly also sub-namespaces like > Acme\Frobulator\Handlers\TextFile\Format\Exceptions, but not in other > parts of Acme\Frobulator\Handlers\TextFile or Acme\Frobulator as a whole. > > If Acme\Frobulator was the root of the Composer package, the only way to > make something internal to the package as a whole would be to flatten > everything into that root namespace, like > Acme\Frobulator\Handler_TextFile_Format_CSV > > That leads me to think that if we do want "module"-level features - be > it internal classes, internal properties, or module-level declare() > settings - we need a way for the user to define that separately from the > fully-qualified namespace. > > Regards, > > -- > Rowan Tommins (n=C3=A9 Collins) > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --00000000000001720505afd522d7--