Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:63063 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41348 invoked from network); 18 Sep 2012 15:14:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2012 15:14:51 -0000 Authentication-Results: pb1.pair.com smtp.mail=amaury.bouchard@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=amaury.bouchard@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.42 as permitted sender) X-PHP-List-Original-Sender: amaury.bouchard@gmail.com X-Host-Fingerprint: 209.85.219.42 mail-oa0-f42.google.com Received: from [209.85.219.42] ([209.85.219.42:42557] helo=mail-oa0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8D/7D-07072-AEF88505 for ; Tue, 18 Sep 2012 11:14:51 -0400 Received: by oagh2 with SMTP id h2so6692424oag.29 for ; Tue, 18 Sep 2012 08:14:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=XJ1xObnuelc3LZ7mTZ110vlikFzIbrRUQ5MNA5HpM34=; b=E/D36rNjW+V6h1FcL51u++xctt5t/ED5z5/NfLZ8+B7qk90rQy2IgHCZpjHCbQNYiu Me6A0bfaFEc+lNoUazkf5C4GVBzjQ2az1500V5jo9PGhZ9cHe8IEuLBUfySwXmDfYl8x rSJwkJwB6cZmQP3AtYQK8NlZHdUeFHg8N1y84i+yHv/Zp8hYQCfYm8muUqRcKOZmpe+/ jNa+KqdZGny4WlqoOf/UT9GD1LKVFh2Oukcd2PVvUziUL24t/G0PZ8Nkt/vgCrIIFBML Ryg6X47n0c1lIk2bgTNUHRlilh1NTwUTrEQo0drL8mU0bZSnjsY7jpL9EMAC22rdmCoo y7rA== Received: by 10.60.170.104 with SMTP id al8mr210194oec.78.1347981287992; Tue, 18 Sep 2012 08:14:47 -0700 (PDT) MIME-Version: 1.0 Sender: amaury.bouchard@gmail.com Received: by 10.182.193.68 with HTTP; Tue, 18 Sep 2012 08:14:27 -0700 (PDT) In-Reply-To: References: Date: Tue, 18 Sep 2012 17:14:27 +0200 X-Google-Sender-Auth: GBXD_seh5zZUt9rfTZ2aLQ6OxZ8 Message-ID: To: Rasmus Schultz Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] constructor hook From: amaury@amaury.net (Amaury Bouchard) Sounds to me like aspect-oriented programming, applied to object construction. Take a look at: https://github.com/AOP-PHP/AOP 2012/9/18 Rasmus Schultz : > 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