Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:15:30+00:00 2026-05-30T15:15:30+00:00

Working with a PHP library class, and I’d like to wrap all of its

  • 0

Working with a PHP library class, and I’d like to wrap all of its public functions in a subclass… Something along the lines of:

class BaseClass
{
   function do_something()
   {
        some;
        stuff;
   }

   function do_something_else()
   {
        other;
        stuff;
   }

   /*
    * 20-or-so other functions here!
    */
}

class SubClass extends BaseClass
{
   function magicalOverrideEveryone()
   {
        stuff-to-do-before;        // i.e. Display header
        call_original_function();  // i.e. Display otherwise-undecorated content
        stuff-to-do-after;         // i.e. Display footer
   }
}

Boiling it down, I’d prefer not to have to override every superclass method with the same wrapper code, if there’s a [somewhat elegant / clean] way to do it all in one place.

Is this possible? I suspect I’m in metaprogramming land here, and don’t even know if PHP offers such a beast, but figured I’d ask…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:15:31+00:00Added an answer on May 30, 2026 at 3:15 pm

    You could do this easily with the __call magic method and a generic “proxy” class which doesn’t inherit directly from the base class.

    Here is a (near) complete implementation of a proxying class which wraps whatever object you pass it. It will invoke some “before” and “after” code around each method call.

    class MyProxy {
      function __construct($object) {
        $this->object = $object;
      }
    
      function __call($method, $args) {
        // Run before code here
    
        // Invoke original method on our proxied object
        call_user_func_array(array($this->object, $method), $args);
    
        // Run after code here
      }
    }
    
    
    $base = new BaseClass();
    $proxy = new MyProxy($base);
    
    $proxy->doSomething(); // invoke $base->doSomething();
    

    You would of course want to add a bit of error handling, like asking the proxied object if it responds to the given method in __call and raising an error if it doesn’t. You could even design the Proxy class to be a base-class for other proxies. The child proxy classes could implement before and after methods.

    The downside is that your “child class” no longer implements BaseClass, meaning if you’re using type-hinting and want to demand that only objects of type BaseClass are passed into a function, this approach will fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Janrains PHP openID library. All is working perfectly except when I
at my working place (php only) we have a base class for database abstraction.
I've been working with PHP lately, and I came across something I couldn't solve.
Working with PHP Xpath trying to quickly pull certain links within a html page.
If you are working in PHP (or I guess any programming language) and using
I am working with php and want to have a page that has a
I'm working with PHP 5.3 on my local machine and needed to parse a
I'm used to working with PHP but lately I've been working with Java and
I've got my php working to set a cookie storing the http_referer the visitor
I am a web-developer working in PHP. I have some limited experience with using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.