Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:63056 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4500 invoked from network); 18 Sep 2012 12:19:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2012 12:19:24 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.220.170 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.220.170 mail-vc0-f170.google.com Received: from [209.85.220.170] ([209.85.220.170:35363] helo=mail-vc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 52/06-07072-AC668505 for ; Tue, 18 Sep 2012 08:19:23 -0400 Received: by vcbfk26 with SMTP id fk26so9267269vcb.29 for ; Tue, 18 Sep 2012 05:19:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type :x-gm-message-state; bh=ClE9gGl04dycF/VERW+/2E860HyuJE4uXPIkwKDe1TM=; b=cD+/TXDYiGyLyrigcTDSv7ELxi1nzpRaOS9PGgTky1horLZ/bS3V/ze2YDm/NRW9Gd C2dTU2HSQ2hulS7vnGRaMzGgI6LJtkvwo+tmtfC0iZ4CPBPOPp9uGCto2pToAAvdQIOf OYZt0FhYY5cIA7etb4Szjz1/Sfx6YlZa/h1wSHLaf3D2phBWw/k4z+lH5qf4vOsR7tBC 3A2dFY8S+8ncOGeUgkZOUKH7ymRwQ+/4EPgO1qgLxCVDZqccy2j9d7xDrg+s8krsYmXD dEdCzBeHjpRBEtuA3x2Xfb7gORdhH/6WacZnkTu3J3ljzoI1XVTdWSvzwjvjHn/aY96O N85A== MIME-Version: 1.0 Received: by 10.52.26.8 with SMTP id h8mr4907175vdg.115.1347970759132; Tue, 18 Sep 2012 05:19:19 -0700 (PDT) Received: by 10.58.91.162 with HTTP; Tue, 18 Sep 2012 05:19:19 -0700 (PDT) Date: Tue, 18 Sep 2012 08:19:19 -0400 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf307c9afe4405b804c9f8e6e8 X-Gm-Message-State: ALoCoQkbAPvfAGeFFNBwXr25o9P/po7oOF9ELrNV0p3LOnsYIlXuNkSpIihZK9y8+svvEdjYtoza Subject: constructor hook From: rasmus@mindplay.dk (Rasmus Schultz) --20cf307c9afe4405b804c9f8e6e8 Content-Type: text/plain; charset=ISO-8859-1 Hey, I'm going to make this brief, because I suspect a lot of people are going to jump at the opportunity to cry bloody murder when I suggest this. I wonder if it would make sense to have a way to globally hook into __construct() - sort of like how you can hook into the autoloaders with spl_autoload_register() ... This could be useful for things like dependency injection and debugging. Lithium (for one) uses a unified constructor to accomplish something like this: http://lithify.me/docs/manual/lithium-basics/architecture.wiki That's bad for various reasons I won't delve into. Various DI containers use a replacement for the "new" keyword, e.g. DI::create('MyClass') which is bad (worse) for other reasons... So basically, what if you could solve these design patterns by hooking into constructors directly instead - something like: constructor_hook_register(function($object) { > echo 'an object of type '.get_class($object).' was constructed!'; > }); > $test = new Foo(); would print: an object of type Foo was constructed! One other area where this could help, is in event-driven systems, where this sort of thing could be used for "co-construction". Of course, you could accomplish the same thing in a more controlled fashion, by simply establishing conventions - for example, DI::inject(new My Class()) would work just as well, it's just inconvenient to type; on the other hand, it's explicit about what's happening. So I'm not at all demanding this feature - I'm merely posting this for debate. Thanks, Rasmus Schultz --20cf307c9afe4405b804c9f8e6e8--