روز برنامه‌نویس مبارک 🤩🎉 از هدایای روز برنامه‌نویس جا نمونی ⌛
۰ ثانیه
۰ دقیقه
۰ ساعت
۱ همایون
جواب تمرین این جلسه
جامعه پی اچ پی ایجاد شده در ۰۸ مرداد ۱۴۰۱

سلام. من به این شکل Class رو نوشتم میشه بررسی کنید بهینه هست یا حالت بهینه‌تر چیه. هر چند این کار اصلا درست نیست چون اگر مقداری داخل QueryParameter ارسال بشه که با Property‌های داخل Class یکی باشه اونوقت Override میشه که میتونه باگ درست کنه. مثلا یک راه حل اینه که یه prefix به key اون مقادیری که توسط QueryParam ارسال میشه اضافه کرد و اونوقت داخل Class ذخیره کرد.


namespace App\\Core;
class Request
{
    private $queryParams = [];
    private $protocol;
    private $domainName;
    private $uri;
    private $url;
    public function __construct()
    {
        $classMethods = get_class_methods($this);
        array_filter($classMethods, function (string $methodName) {
            if (strpos($methodName, '_data_') !== false) {
                $this->$methodName();
            }
        });
    }
    public function get(string $key): string | null
    {
        $value = $this->{$key} ?? false;
        if (!$value) {
            $value = null;
        }
        return $value;
    }
    public function isExists(string $key): bool
    {
        return isset($this->{$key});
    }
    public function getQueryParams(string $key = ''): string | array | null
    {
        if ($key !== '') {
            return $this->queryParams[$key] ?? null;
        }
        return $this->queryParams;
    }
    private function _data_getProtocol()
    {
        $this->protocol = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on' ? 'https' : 'http';
    }
    private function _data_getDomainName()
    {
        $this->domainName = $_SERVER['SERVER_NAME'];
    }
    private function _data_getUri()
    {
        $this->uri = $_SERVER['REQUEST_URI'];
    }
    private function _data_getUrl()
    {
        $this->url = $this->protocol . '://' . $this->domainName . $this->uri;
    }
    private function _data_getQueryParams()
    {
        $queryString = explode('?', $this->uri);
        $keyValues = explode('&', $queryString[1]);
        foreach ($keyValues as $_keyValue) {
            $key = strstr($_keyValue, '=', true);
            $value = ltrim(strstr($_keyValue, '='), '=');
            if ($key !== '') {
                $this->queryParams[$key] = $value;
            }
        }
    }
    private function _data_setQueryParamsAsProp()
    {
        foreach ($this->queryParams as $key => $value) {
            $queryParamKey = '__' . $key;
            $this->$queryParamKey = $value;
        }
    }
}
 # Front Controller
use \\App\\Utilities\\UrlUtility;
use \\App\\Core\\Request;
use \\App\\Core\\Router;
include_once __DIR__ . DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR . 'constants.php';
include_once BASE_DIR . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$request = new Request();
print_r($request);
$route = UrlUtility::getRoute();
$viewRouter = new Router($route);
$viewRouter->init();

سلام دوست عزیز

بله اکیه اما یکم جلوتر برید مباحثی رو یاد می‌گیرید که کدنویسی‌تتون رو تمیز ترمی‌کنه

موفق باشید ?

بهترین پاسخ
محمد گازری ۰۸ مرداد ۱۴۰۱، ۰۷:۳۶