Reflection API Questions



Dear PHP Users,

I have two 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

Relevant Pages

  • Re: [PHP] Reflection API Questions
    ... Check out the runkit extension to PHP. ... Jim Wilson wrote: ... Obviously I'm not looking for a way to modify class A's source code, ... I know that PHP supports extending classes and interfaces, ...
    (php.general)
  • Re: tools to scan source code
    ... I've been evaluate SWAAT and it is adequately meet my needs. ... I haven't try RATS, but I will try it soon. ... (but it's not dependable it just analyzes source code ... >> PHP is fairly C-like. ...
    (Pen-Test)
  • FW: [PHP] Re: Please hack my app
    ... My name is Jordan Forssman, I am representing a company called Armorize ... We have developed a source code analysis platform for PHP, ... Armorize Technologies ...
    (php.general)
  • Re: Python gets macros - now XML does too
    ... of PHP, which was originally a just simple HTML templating language. ... wanted to open the source of Metastorage in order to attract as many ... that are actual source code of the described classes in meta-programming ...
    (comp.lang.lisp)
  • Re: The Inquirer
    ... >>I could write a compiler for an arbitrary language and put this to open ... > Now go to the url with whatever web browser your using. ... PHP is open source. ... that the actual source code that makes ...
    (comp.os.vms)