Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115854 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 12225 invoked from network); 26 Aug 2021 13:13:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Aug 2021 13:13:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2F0D31804D8 for ; Thu, 26 Aug 2021 06:47:57 -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=0.6 required=5.0 tests=BAYES_50,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-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (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, 26 Aug 2021 06:47:56 -0700 (PDT) Received: by mail-lf1-f44.google.com with SMTP id j4so6950366lfg.9 for ; Thu, 26 Aug 2021 06:47:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=EVOjKpumfxdxSslfs+SIRfor9BDlabvzjMcE5ndg4hs=; b=Ku6RTyDOg3uIjE75c+2jbyj/lg2XxEPaYeMC1vBWxkdC2Dx3WYAs0pJV8BXJntefgc nSHHkkiYZHOuEHZ2ocw5jU8x4QZFX+Q8tt0z+emF/p2f38ql0CUIZUu+QV7Hd3AHmHMc aKdpW177xR4unHyNWfpwqUN/sUHC3r5qEa04BmN/neAHsnPrOYiGp4PJ5dq7fOikpBKy dM18LqkrkmbXXP/HvSKycrviYeCEeG0OZ9R1PxsTJZTkYzbUYdJQk6IH1GczO1agTV4m caiMlrGzadlDuaFKVwxu21MfwDJYeHe7ahMBQDCXM0TAt7o/dmxJBVSWqqi1PEDErp5C 9aTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=EVOjKpumfxdxSslfs+SIRfor9BDlabvzjMcE5ndg4hs=; b=SlgCRl44d4o2FQbYvB5uYqwUh8eQDtEc+YJqwZfrYUlQDTY7hgZutd2wqJ3pnmopS9 uDhf4drnhDRUbV2V2NFsTkbIXizRD6YWGnarSFYZM2M/vefBRUKjbbrz9cUzVLI/R8mw GTLjv+T1j/xoDGcCxDApHnMR4J3n3jJqE5yUxylHN/l9yRwwMguCGhaYUX6Rywhoyhvd Z2yLwhTvFvwPJ9rMnbfYper/VOfs5mdAiq6UDuBUW58E7MOwZAZFEbNaJzPqdMd/UgAH bmzlX4ShI+/0wDuXCxxyiyA2X21ArQL+y9Y7bfsqpyj9I2DuyyJ8QsSKAQWiCJIW47vc kuDw== X-Gm-Message-State: AOAM532NmWwnFwUlfO63Wc5Jr8EPo8i9gnnwLCTa979KhG9EyRz1zDtY ZalSQm2MEewodprA1WseMDYQ2XhmEa8NV9/Yd9u7UE34yn8= X-Google-Smtp-Source: ABdhPJxal24ugdgc2mKP+Thym6PlEcRHDSNQvkd2qxFKlxWW0LN8oXDsSQQZn1JN8lfVHNqu9DW0DHrHfHBBN/2i9JA= X-Received: by 2002:a19:7b17:: with SMTP id w23mr2753627lfc.61.1629985672845; Thu, 26 Aug 2021 06:47:52 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ab3:6384:0:0:0:0:0 with HTTP; Thu, 26 Aug 2021 06:47:52 -0700 (PDT) Date: Thu, 26 Aug 2021 15:47:52 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Alternatives for encapsulation when using composition instead of inheritance From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) Hi list! Don't know if this already exists, but maybe we could consider brainstorming or gather alternatives for different types of encapsulation when using composition? Currently, encapsulation like private and protected is only possible when using inheritance - when using composition, that is, class A is using (aggregating) class B, your only alternative is public, so full visibility (or getters/setters, which also is full visibility, since you can't limit who's using those methods). There are four alternatives at least: * Friend classes, like in C++: Class B is annotated to be a friend of A, so class A can see the content of class B but no other class can. * Package private, like in Java: Class A and B are defined in the same package, so they can see each other's fields that are defined as "internal" * Namespace private: Class A and B are defined in the same namespace, so they can see each other's properties that are defined as "internal" * Attribute for namespace private: Like above, but not possible right now since it requires that namespace is added to the PHP runtime Functionality like this should not be handled by static analysis, because library authors need to be able to trust it. Psalm supports already @internal and @psalm-internal, but of course not everyone is using Psalm/Phpstan/other analyzer. The problem with package private is that PHP does not have packages. The problem with namespace private is that it cannot handle namespace siblings. Another problem is that it can be "hacked" by client code if the client code decides to use another vendor's namespace (a weird thing to do, though). The run-time hit should not be bigger, since we're already checking each object property for private. Correct me if I'm wrong. The risk/benefit/effort should be considered for all alternatives. I'd argue that something is better than nothing, so far as it does not block future work. :) Maybe adding namespaces to runtime could be a first step? Olle