Created
July 20, 2021 09:10
-
-
Save Ichinya/31a9bfe0df5758a6e1ee2267880a8064 to your computer and use it in GitHub Desktop.
Восстанавливаем объект из сессии
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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