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 6913911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:16:44+00:00 2026-05-27T09:16:44+00:00

This is mostly an experiment , but I do think it does have some

  • 0

This is mostly an experiment, but I do think it does have some practical uses.

The idea is to be able to patch a PHP script at runtime without have to reload the request.

For example, let’s say I wrote a WebSocket server in PHP, and this server is running the following class…

class MyServerApp extends WebSocketServerApp {
    protected $clients = array();

    public function onConnect($client){
        $this->clients[$client->getId()] = $client;
    }

    public function onDisconnect($client){
        unset($this->clients[$client->getId()]);
    }

    public function onData($client, $data){
        $client->send($data); // perform echo functionality
    }

}

Basically, the server creates a single instance of MyServerApp class and calls it appropriately. The above app is an echo server; it responds to all client request with exactly what they asked for.

Now let’s say I modified the server source code and want to keep the existing server running, but change behaviour (so as to not loose the existing clients). The app server conveniently has an onTick() event, which we can use to check changes to the source code:

class MyServerApp extends WebSocketServerApp {
    // the existing code from above goes here

    /**
     * @var integer Timestamp of when the server was last patched.
     */
    public $last_patch = 0;

    public function __construct(){
        $this->last_patch = time();
    }

    public function onTick(){
        if($this->last_patch < filemtime(__FILE__)){
            // include __FILE__;
        }
    }
}

The patching check will probably work nicely, but the actual patching method won’t (it is currently commented, by the way).
The main reason is that the server class has already been defined.

So how would you do the actual patching? Somehow overwrite functions, or classes?

Possible Solutions

  • Use a better suited language (such as js/node.js) which allows overwriting (@teresko)
  • Use classkit (and/or alternatives) to overwrite PHP functions/classes (@MatějZábský)
  • Offload server logic to anonymous functions and simply overwrite such functions*
  • Rename duped classes and load them normally (definitely leaks memory though) (@chris)

[*] server.php would look like:

$server['onData'] = function(){ /* new function body */ };

Notes

On the other hand, this architecture poses some issues that really need to be tended to:

  • permanent damage to the running server
  • memory leaks (some resources will get leaked, probably including functions)
    • (does PHP have a GC for functions?)
  • 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-27T09:16:45+00:00Added an answer on May 27, 2026 at 9:16 am

    You may want to have a look at classkit (its function classkit_method_redefine and classkit_import in particular).

    But that’s really horrible way to go. Are you sure you can’t afford to have a millisecond down-time while restarting the script (as everyone does)?

    This may also not be sustainable in long term (I wouldn’t be surprised if the overwritten method was just leaked). Also, I’m not sure if the tick callback is really built for use on production server (it may have some performance overhead).

    EDIT:

    Sadly, there is rather limited number of articles about PHP’s garbage collector. Here is one (it is a part of series on performance optimizations in PHP), but it doesn’t go into details on how functions or types are treated.

    From my experience, PHP scripts running continuously for days need to be restarted every few days (I use Cron-job which tries to start the script every few minutes along with a lock file), because after that, the script becomes less and less stable.

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

Sidebar

Related Questions

I think this mostly applies to web applications, since you often see things like
I have this thing working mostly. What I don't get is, if I have
This is the situation : I mostly program C# and have written types in
I believe this mostly applies to custom dialog boxes being put into DLLs, but
This is mostly a theoretical question I'm just very curious about. (I'm not trying
This is mostly just out of curiosity, and is potentially a silly question. :)
This is mostly a data warehouse philosophy question. My project involves an Oracle forms
I'm starting on a new iPhone project, and this application mostly relies on MySQL.
This question is mostly academic. I ask out of curiosity, not because this poses
I am mostly following this page: http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html I used this command to create the

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.