using __call() to simulate extending classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • surf

    using __call() to simulate extending classes


    We have some open source tools that we use. I don't want to extend
    the classes in the tools source because the calls to new() happen in
    the tool source and we are supposed to avoid changing the code there
    as much as possible, rather I tried to adopt a strategy where I have a
    base class variable in my class and I direct all calls I don't want to
    handle to the base class through call_user_func_ array() after first
    intercepting them through __call. I use __get and __set similarly. I
    think I got this idea from the PHP cookbook, but I don't have it in
    front of me.

    Anyway, one problem I found with this approach is that if the base
    class defined a pass by reference paramter
    such, I'm not sure how to make that work by propgating through my
    __call() as apbass by reference.
    I got around it for now by declaring each of those functions in my
    class directly and calling the base class.

    Another problem is that if I pass any call onto the base class, if
    some other call farther down the call stack calls a function and I
    want it to call back into my class because I have my own definition
    for that function, it won't work because the base class doesn't know
    about my class and I don't know a good way to do this which I didn't
    realize until it came up as an issue. It happens in once place so I
    have to copy the code from the tool source and have my own copy of the
    top level function. In Perl I think I can put whatever I want into
    someone elses namespace, I'm not sure if I can override a method in a
    class that someone else declared in PHP which is essentially how it
    might have been done in Perl

  • Janwillem Borleffs

    #2
    Re: using __call() to simulate extending classes

    surf wrote:
    I'm not sure if I can override a method in a
    class that someone else declared in PHP which is essentially how it
    might have been done in Perl
    >
    When you are using PHP4, you can extend classes as you would do in Java:

    class Foo extends Base {}

    Afterwards, you can declare the methods you want to override, and leave out
    the ones you want to use unchanged from the base class.

    In PHP5, classes and methods can be declared final, which means that you
    cannot extend/override them this way.


    JW


    Comment

    Working...