# Reflection API Injection Mechanism

- Thread: https://externals.io/message/27946
- Original archive: https://news-web.php.net/php.internals/27946
- Started by Jim Wilson on 2007-02-09T16:18:03+00:00
- Messages: 3
- 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

---

## #27946 — Jim Wilson — 2007-02-09T16:18:03+00:00

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

Dear PHP Internals subscribers,

I have two quick questions regarding PHP 5's Reflection API (I apologize if
this isn't the right list to ask):

1) Is there a way to inject a method into a Class such that future
instantiations of that Class will have the method?  For example:

------------ Before -----------
class A {
    // Nothing
}
------------ Before -----------

------------ After -----------
class A {
    // method which was injected
    public function injectedFunc($someArg) {
        // ....
    }
}
------------ After -----------

Obviously I'm not looking for a way to modify class A's source code, just
the effective class A specification.

I know that PHP supports extending classes and interfaces, but in my
use-case that isn't really an option since I don't control the code which
instantiates the objects, and they're not using any kind of Factory pattern
for object construction that I could overload with my extended
implementation.

2) Is there a way to change a method which already exists, as in by
overwriting it with a new function?  I'm looking to do the equivalent of
JavaScript's 'function as member' treatment, where it's easy to do something
like:

------------ Snip -----------
// Obj is some object
Obj.meth = function (arg) { /* do something with arg */ };

// Then later on ...
Obj.meth('hello there');
------------ Snip -----------

Thanks in advance for any help, or for redirecting me to the correct list if
this isn't it.

-- Jim R. Wilson

---

## #27951 — Christian Schneider — 2007-02-09T17:10:14+00:00

URL: https://externals.io/message/27946#27951 · In reply to: #27946

Jim Wilson wrote:
> 1) Is there a way to inject a method into a Class such that future
> instantiations of that Class will have the method?  For example:
> 
> 2) Is there a way to change a method which already exists, as in by
> overwriting it with a new function?  I'm looking to do the equivalent of
> JavaScript's 'function as member' treatment, where it's easy to do
> something

See php.net/runkit

- Chris

---

## #27953 — johannes@php.net — 2007-02-09T17:15:52+00:00

URL: https://externals.io/message/27946#27953 · In reply to: #27946

Hi Jim,

you're searching for runkit ;-)
http://de.php.net/manual/en/ref.runkit.php

johannes


> Dear PHP Internals subscribers,
> 
> I have two quick questions regarding PHP 5's Reflection API (I apologize if
> this isn't the right list to ask):
> 
> 1) Is there a way to inject a method into a Class such that future
> instantiations of that Class will have the method?  For example:
> 
> ------------ Before -----------
> class A {
>     // Nothing
> }
> ------------ Before -----------
> 
> ------------ After -----------
> class A {
>     // method which was injected
>     public function injectedFunc($someArg) {
>         // ....
>     }
> }
> ------------ After -----------
> 
> Obviously I'm not looking for a way to modify class A's source code, just
> the effective class A specification.
> 
> I know that PHP supports extending classes and interfaces, but in my
> use-case that isn't really an option since I don't control the code which
> instantiates the objects, and they're not using any kind of Factory pattern
> for object construction that I could overload with my extended
> implementation.
> 
> 2) Is there a way to change a method which already exists, as in by
> overwriting it with a new function?  I'm looking to do the equivalent of
> JavaScript's 'function as member' treatment, where it's easy to do something
> like:
> 
> ------------ Snip -----------
> // Obj is some object
> Obj.meth = function (arg) { /* do something with arg */ };
> 
> // Then later on ...
> Obj.meth('hello there');
> ------------ Snip -----------
> 
> Thanks in advance for any help, or for redirecting me to the correct list if
> this isn't it.
> 
> -- Jim R. Wilson
