Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

1.2k Views Asked by At

I migrated to php 8.1 and is giving this error: Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

<?php
namespace core;

use src\Config;

class Request {

    public static function getUrl() {
        $url = filter_input(INPUT_GET, 'request');
        $url = str_replace(Config::BASE_DIR, '', $url);
        return '/'.$url;
    }

    public static function getMethod() {
        return strtolower($_SERVER['REQUEST_METHOD']);
    }

}

How can I resolve this error?

<?php
namespace core;

use src\Config;

class Request {

    public static function getUrl() {
        $url = filter_input(INPUT_GET, 'request');
        $url = str_replace(Config::BASE_DIR, '', $url ?? "");
        return '/'.$url;
    }

    public static function getMethod() {
        return strtolower($_SERVER['REQUEST_METHOD']);
    }

}

I put the ?? "" but it does not work

0

There are 0 best solutions below