class Core{
private $db; // подключаем бд
// Даем переменные
private $userData = array(); // Все данные пользователя
// Главные функции тут
public function __construct() {
$this->authorize(); // Авторизация пользователя
}
// Проверка авторизации
private function authorize() {
if (isset($_COOKIE['autes'])):
$cookieToken = $_COOKIE['autes'];
$req = DB::$dbs->query('SELECT * FROM `user` WHERE `autes` = ? LIMIT 1',[$cookieToken]);
if ($req->rowCount()):
$this->userData = $req->fetch();
else:
setcookie('autes','',time()-3600,'/');
endif;
endif;
}
public function getData()
{
return $this->userData ?? null;
}
}
$core = new Core();
$data = $core->getData():
function foo($args1, $args2) {
$html = '';
foreach ($args1 as $val) {
$html .= $val;
}
return $html;
}
echo foo($args1, $args2);
$user = [
'id' => 1,
'name' => 'Ivan Ivanov',
'role' => 'developer',
'salary' => 100
];
$apiTemplatesSet1 = [
'/api/items/%id%/%name%',
'/api/items/%id%/%role%',
'/api/items/%id%/%salary%'
];
class Invoker
{
private $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function __invoke(array $key): string
{
return $this->data[$key[1]];
}
}
$function = new Invoker($user);
$result = array_map(function ($key) use ($function)
{
return preg_replace_callback('#%(.*)%#isU', $function, $key);
}
, array_values($apiTemplatesSet1));
echo '<pre>' . print_r($result, true);
$balance = json_decode($json, true);
echo $balance['result']['balance'];