# constructor hook

- Thread: https://externals.io/message/63056
- Original archive: https://news-web.php.net/php.internals/63056
- Started by Rasmus Schultz on 2012-09-18T12:19:19+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #63056 — Rasmus Schultz — 2012-09-18T12:19:19+00:00

URL: https://externals.io/message/63056#63056

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:

<?php
> 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

---

## #63059 — johannes@schlueters.de — 2012-09-18T13:07:13+00:00

URL: https://externals.io/message/63056#63059 · In reply to: #63056

> 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.

I don't think it's a good thing to have. "new" right now gives you a 
clear idea whats going on. Magic behind the scenes is hard to debug.

If you really want it: https://github.com/johannes/php-test-helpers

johannes

---

## #63060 — Kris Craig — 2012-09-18T13:18:08+00:00

URL: https://externals.io/message/63056#63060 · In reply to: #63059

On Tue, Sep 18, 2012 at 6:07 AM, Johannes Schlüter
<johannes@schlueters.de>wrote:

> 
> > […]
>

HELP!  BLOODY MURDER!!!!


(Sorry).

--Kris

P.S. Why did this have to happen now?!  He was just three days away from
retirement....

P.P.S. Now I'm gonna find the *real* killer!

P.P.P.S. (Sorry again).

---

## #63061 — Ferenc Kovacs — 2012-09-18T13:48:59+00:00

URL: https://externals.io/message/63056#63061 · In reply to: #63060

> On Tue, Sep 18, 2012 at 6:07 AM, Johannes Schlüter
> <johannes@schlueters.de>wrote:
>
> > […]
> going
> > […]
>
> HELP!  BLOODY MURDER!!!!
>
>
> (Sorry).
>
> --Kris
>
> P.S. Why did this have to happen now?!  He was just three days away from
> retirement....
>
> P.P.S. Now I'm gonna find the *real* killer!
>
> P.P.P.S. (Sorry again).
>

I find your recent activity on this list pretty unconstructive:
http://www.mail-archive.com/internals@lists.php.net/msg61060.html
http://www.mail-archive.com/internals@lists.php.net/msg60923.html
http://www.mail-archive.com/internals@lists.php.net/msg60824.html
http://www.mail-archive.com/internals@lists.php.net/msg60668.html
the last/oldest not completely off-topic, but you still think that "What's
my point? I'm not sure that I have one--"
please don't burden the subscribers of the list with these kind of emails.
thanks.


-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #63062 — Edmond [HT] — 2012-09-18T15:08:41+00:00

URL: https://externals.io/message/63056#63062 · In reply to: #63056

Hello Rasmus!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RS> Hey,

RS> I wonder if it would make sense to have a way to globally hook into
RS> __construct() - sort of like how you can hook into the autoloaders with
RS> `spl_autoload_register()` ...

RS> This could be useful for things like dependency injection and debugging.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Programming language should have a clear behavior.
But here is too much magic.

Usually, DI objects are not created in a vacuum.
They require a factory... may be service locator.

Like this: http://snipt.org/vLae1/Traditional

-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Regards, Edmond
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

---

## #63063 — Amaury Bouchard — 2012-09-18T15:14:27+00:00

URL: https://externals.io/message/63056#63063 · In reply to: #63056

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 <rasmus@mindplay.dk>:
> 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:
>
> <?php
> > […]
>
>
> 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

---

## #63064 — jpauli — 2012-09-18T16:22:37+00:00

URL: https://externals.io/message/63056#63064 · In reply to: #63063

> Sounds to me like aspect-oriented programming, applied to object construction.
> Take a look at: https://github.com/AOP-PHP/AOP

+1, this is the same case as AOP, but for constructor.
Thus AOP may fit the need.

Julien.P

---

## #63075 — Ralph Schindler — 2012-09-18T17:10:35+00:00

URL: https://externals.io/message/63056#63075 · In reply to: #63064

This is one of the main use cases of AOP.  Demonstrated by this script:

     <?php

     class Bar {}
     class Foo {
         public function __construct(Bar $b) {
             echo 'instance of ' . get_class($b);
         }
     }

     function doDi($joinpoint) {
         // do smart stuff, then call:
         $joinpoint->setArguments(array(new Bar));
     }

     aop_add_before('Foo->__construct()', 'doDi');

     $f = new Foo; // 'instance of Bar'


-ralph



> 
> > […]
>
> +1, this is the same case as AOP, but for constructor.
> Thus AOP may fit the need.
>
> Julien.P

---

## #63121 — Rasmus Schultz — 2012-09-18T22:46:02+00:00

URL: https://externals.io/message/63056#63121 · In reply to: #63064

Good point - I agree. Thanks for taking the time to think about this!



> On Tue, Sep 18, 2012 at 5:14 PM, Amaury Bouchard <amaury@amaury.net>
> wrote:
> > […]
> construction.
> > […]
>
> +1, this is the same case as AOP, but for constructor.
> Thus AOP may fit the need.
>
> Julien.P

---

## #63123 — Kris Craig — 2012-09-19T02:31:00+00:00

URL: https://externals.io/message/63056#63123 · In reply to: #63063

> Sounds to me like aspect-oriented programming, applied to object
> construction.
> Take a look at: https://github.com/AOP-PHP/AOP
>
>
Yeah I wasn't aware of this, either, but I can think of several scenarios
in which it'd be useful.   I know this is PECL, but is this something that
might be worth bundling?  I'm still a bit new here so if that idea's heresy
please let me know.  If not, that would be a nice feature to have on a
default installation I think.  Thoughts?

<tangent>@Ferenc Yes congrats on cherry-picking a few recent posts that
weren't Nobel Prize material (some of which did make constructive points
when viewed in context, albeit with some creative flair).  I'm not in the
market for a nanny right now; I'll let you know if that ever changes.  If
you'd like to discuss your personal feelings about this further, please do
so via private email so we don't spam the list.</tangent>

--Kris

---

## #63159 — jpauli — 2012-09-19T17:34:15+00:00

URL: https://externals.io/message/63056#63159 · In reply to: #63123

>
> On Tue, Sep 18, 2012 at 8:14 AM, Amaury Bouchard <amaury@amaury.net>
wrote:
>
> > […]
> Yeah I wasn't aware of this, either, but I can think of several scenarios
> in which it'd be useful.   I know this is PECL, but is this something that
> might be worth bundling?  I'm still a bit new here so if that idea's
heresy
> please let me know.  If not, that would be a nice feature to have on a
> default installation I think.  Thoughts?

Why not, 2 solutions here :
- Bundle it with PHP sources
- Merge it with core (trunk)

I think that it may need more code to bind AOP functionnalities to
Reflection though.

Anyway, an RFC will be needed ;)

Julien.Pauli


> On Tue, Sep 18, 2012 at 8:14 AM, Amaury Bouchard <amaury@amaury.net>
> wrote:
>
> > […]
> Yeah I wasn't aware of this, either, but I can think of several scenarios
> in which it'd be useful.   I know this is PECL, but is this something that
> might be worth bundling?  I'm still a bit new here so if that idea's heresy
> please let me know.  If not, that would be a nice feature to have on a
> default installation I think.  Thoughts?
>
> <tangent>@Ferenc Yes congrats on cherry-picking a few recent posts that
> weren't Nobel Prize material (some of which did make constructive points
> when viewed in context, albeit with some creative flair).  I'm not in the
> market for a nanny right now; I'll let you know if that ever changes.  If
> you'd like to discuss your personal feelings about this further, please do
> so via private email so we don't spam the list.</tangent>
>
> --Kris

---

## #63172 — Kris Craig — 2012-09-19T18:37:02+00:00

URL: https://externals.io/message/63056#63172 · In reply to: #63159

>
> 
> > […]
> wrote:
> > […]
> that
> > […]
> heresy
> > […]
>
> Why not, 2 solutions here :
> - Bundle it with PHP sources
> - Merge it with core (trunk)
>
> I think that it may need more code to bind AOP functionnalities to
> Reflection though.
>
> Anyway, an RFC will be needed ;)
>
> Julien.Pauli
>
 Agreed.

--Kris
