Skip to content

Instantly share code, notes, and snippets.

@Ichinya
Created July 20, 2021 09:10
Show Gist options
  • Save Ichinya/31a9bfe0df5758a6e1ee2267880a8064 to your computer and use it in GitHub Desktop.
Save Ichinya/31a9bfe0df5758a6e1ee2267880a8064 to your computer and use it in GitHub Desktop.
Восстанавливаем объект из сессии
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public static function currentUser(): User
{
return self::reCreate($_SESSION['auth']);
}
private static function reCreate($obj)
{
$obj = (array)$obj;
$newObj = new User();
foreach ($obj as $k => $v) {
$parts = explode(chr(0), $k);
if ($k != '__PHP_Incomplete_Class_Name') {
if ($parts[0] == $k)
$newObj->reCreatePrivate($k, $v);
else
$newObj->reCreatePrivate($parts[2], $v);
}
}
return $newObj;
}
private function reCreatePrivate($name, $value)
{
$this->$name = $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment