Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114214 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 40368 invoked from network); 27 Apr 2021 21:43:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Apr 2021 21:43:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 71B7C1804BD for ; Tue, 27 Apr 2021 14:47:36 -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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) (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 ; Tue, 27 Apr 2021 14:47:36 -0700 (PDT) Received: by mail-qv1-f50.google.com with SMTP id i11so24289972qvu.10 for ; Tue, 27 Apr 2021 14:47:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=i54/c1ezAtbTxqnr8M93W1vgMktPUb7DfH3BXIkcoTM=; b=JvD9QPD4VDu38FpLCWdaFCvorcSTrmGEUAMEp/gWQTLiSxB3Br7UnFy2yImwUtPYMt /jU3nTDqcn4S/kcPQEBsrLW+7EWjnvrjmHQ1KFMnozCmUVLpCGeMRVHvS5Lkn9utgodY 0ZoDFOim5Scqejl0trQXun4obOU1ulks95nOlvu/oNSz3QpIsIa3/AJG5WcHyMBh7Iem V35Y9N43lL63lzFsZ1QRnkPIqVK/7UsBsi9NEHJDX1IGmsx4mR8lsHUDVOl9O1JEM43K 7Qc21d9OZEBB8UaOYQkLSDEZ5612T0PLDp+yjjC49VY0uNZ6STosXODOwxoZ1c7lt9Xu Zhdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=i54/c1ezAtbTxqnr8M93W1vgMktPUb7DfH3BXIkcoTM=; b=Ajx8Omr39l5jtAHeQF2xIrosQmJ+ndNnmPoZJDHr5QRQKBXEWM7vPzmpnjLmfMKY8F 8dKtoJIw0vgzm3Rv99DvKWS+btLL4vfWdRwkYwgIXSj1fTECWsEixowt+eCRkceMM9sK H1iQWlID0++SyDDqZWUme2O/1ABp/YvenKaQbuDDH5zrgJ+EMX/JC/tL8sZY9QiGPCCq D00i/F8uWspV1Z8+GGvp8DY+EbNbYvEzhmIDZtxxh4WrpEn2u338K004NPjI5Q8fz2oe QhYbFNOMT/8YfDKZocjjAs82Yz3bNY3YlJaxvRuwOcIpUUVRTsvDLF5zXFJ8vysz56xl Gcgg== X-Gm-Message-State: AOAM533vvtbdfyxd+O7V1qxpqV6sgGPNYnz7gDGg9Y8QEgFmcNmx3w/O 7MAdeFGUXACoP4L7Y0HrpLox/A== X-Google-Smtp-Source: ABdhPJy1YC9HmNHhU0qU+yYVnS8JWiPkSeulRelVPgqpshukAaCOJFhKo8AbMvYC05yK3LZoIwTa7A== X-Received: by 2002:a0c:b294:: with SMTP id r20mr25406358qve.16.1619560052237; Tue, 27 Apr 2021 14:47:32 -0700 (PDT) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id l199sm3675080qke.118.2021.04.27.14.47.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Apr 2021 14:47:31 -0700 (PDT) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_F22E0BD4-FFC9-4215-9451-591E2739078E" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.6\)) Date: Tue, 27 Apr 2021 17:47:29 -0400 In-Reply-To: Cc: PHP Internals To: Chase Peeler References: <5b9f1500-615a-48f1-815f-1d48b327ef90@processus.org> <179049b1475.11134368b213512.254739612773841999@void.tn> <722ed544-69e3-3be4-f828-185914617228@processus.org> X-Mailer: Apple Mail (2.3608.120.23.2.6) Subject: Re: [PHP-DEV] [RFC][Draft] Sealed Classes From: mike@newclarity.net (Mike Schinkel) --Apple-Mail=_F22E0BD4-FFC9-4215-9451-591E2739078E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Apr 27, 2021, at 3:07 PM, Chase Peeler = wrote: >=20 > As someone mentioned above, maybe they want to just add some logging > capabilities to maybe. > class MyMaybe extends Maybe { > protected $logger; > public function setLogger($logger){ $this->logger =3D $logger; } > public function value(){ > if(null !=3D=3D $this->logger){ $this->logger->log("getting = value"); } > return parent::value(); > } > } That is one argument. But maybe that argument points to the need for a = different feature, one that could be considered for PHP instead of = limiting the freedom of the library/framework author to decide how their = code will be used? We could add an Events feature that would allow users of a class to run = a closure before or after every time a class' methods are run? People = often suggest that attributes can be used to implement such features but = then attributes can only be added by the author of the class, not = injected by users of the class. Imagine if something like the following existed (I just spitballed the = syntax as a straw man suggestion; I doubt an actual events feature would = look much like this after requirements were fleshed out): /*=20 * Attach an event to run *after* every time the Maybe class was = instantiated */ \PHP\Events::after( [ Maybe::class, "__construct" ], func($context){ // Attach a property to the instance that would only be visible = inside of events $context->logger =3D MyLogger::get(); }) /*=20 * Attach an event to run *before* every time the value() method was = called an a Maybe instance */ \PHP\Events::before( [ Maybe::class, "value" ], func($context){ // Call log() on the attached $logger property $context->logger->log("getting value"); }) /*=20 * Your logger class */ class MyLogger { private static $logger; public static function set($logger){ self::$logger =3D $logger; } public static function get(){ if (!isset(self::$logger)) { self::$logger =3D new MyLogger(); } return $logger; } public static function log($msg){ printf( "$1\n", $msg ); } } /*=20 * Example usage code */ $m =3D new Maybe(1) echo $m->value(); // echos "getting value\n1" In my view this would be more consistent with the open-closed principle = of S.O.L.I.D. because it would allow a developer to augment classes in = libraries and frameworks that hardcode instantiate =E2=80=94 do not just = dependency injection =E2=80=94 something you cannot do with mere = subclassing. I know this suggestion is off-topic for this thread, but I introduce to = point out at least some of the objections to `sealed` could addressed = better by new language features. =20 SO IF people are interested in discussing this specific language feature = more, PLEASE break this off into a different thread. -Mike P.S. Also, I admit I did what I hate when other people do it; I focused = on your example that you used to illustrate a general principle and I = addressed that example only, not your general concern. But I only did so = because other features would better address that specific example. =20 Do you have other examples that would illustrate why you might want to = extend a Maybe class that could not be handled with events? --Apple-Mail=_F22E0BD4-FFC9-4215-9451-591E2739078E--