Wednesday, January 3, 2024

Using both static and dynamic versions of the same class

During my web app refactoring, I have to apply changes incrementally instead of changing everything at once. One of my main updates is making classes static. This change usually affects hundreds of files that access properties via magic methods. To avoid changing only the part I am focused on, I use the following strategy to be able to use properties of the class both in a static and dynamic way:
class Request {
  public static $get = array();

  private function __construct() {
    // Set the instance properties so that they can be accessed dynamically
    // until I convert every instance of request property use to static, e.g. Request::$get
    $this->get = &self::$get;
  }

  public static function create() {
    //Sanitize input data, primarily to prevent issues like cross-site scripting (XSS)
    $_GET = self::clean($_GET);
    self::$get = $_GET;
    return new self();
  }
  ...
The only global change I have to make is using Request::create() instead of new Request() to obtain request object.

No comments:

Post a Comment