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
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
On Tue, Sep 18, 2012 at 6:07 AM, Johannes Schlüter
johannes@schlueters.dewrote:
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.
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).
On Tue, Sep 18, 2012 at 6:07 AM, Johannes Schlüter
johannes@schlueters.dewrote: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.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:
internals@lists.php.net/msg61060.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg61060.html
internals@lists.php.net/msg60923.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg60923.html
internals@lists.php.net/msg60824.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg60824.html
internals@lists.php.net/msg60668.html" rel="nofollow" target="_blank">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
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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
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
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
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
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
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: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
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
On Tue, Sep 18, 2012 at 8:14 AM, Amaury Bouchard amaury@amaury.net
wrote:Sounds to me like aspect-oriented programming, applied to object
construction.
Take a look at: https://github.com/AOP-PHP/AOPYeah 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:Sounds to me like aspect-oriented programming, applied to object
construction.
Take a look at: https://github.com/AOP-PHP/AOPYeah 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
On Tue, Sep 18, 2012 at 8:14 AM, Amaury Bouchard amaury@amaury.net
wrote:Sounds to me like aspect-oriented programming, applied to object
construction.
Take a look at: https://github.com/AOP-PHP/AOPYeah 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
Agreed.
--Kris