PHP - loaded before unserialize()

octofox·2021년 5월 30일
0

PHP - errors

목록 보기
1/2

에러

PHP Notice: WEB\Authentication::isLoggedIn(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "WEB\Entity\User" of the object you are trying to operate on was loaded before unserialize() gets called or provide an autoloader to load the class definition in /WEB/classes/WEB/Authentication.php on line 3, referer: https://WEB.WEB.net/login

에러가 일어난 경로는 무시해주길 바란다.
해당 에러에서

Please ensure that the class definition "WEB\Entity\User" of the object you are trying to operate on was loaded before unserialize() gets called or provide an autoloader to load the class definition in ...

이 부분을 유심히 보았다.

Please ensure object you are trying to operate on was loaded before unserialize()

unserialize()를 부르기 전에 객체가 먼저 불러졌는지 확인해라 = 객체를 먼저 부르고 unserialize가 되어야한다.

나는 unserialize() 라는 함수를 호출 한적이 없는데? 하고 생각했다.

스택오버플로우 php 고수님의 답변을 보면

The automatic serialization/unserialization occurs when you call session_start().

라고 알려주신다.

나는 session_start()를 2번 불러오고 있었다.
index.php 첫 줄에서 한번 (index.php에서 라우트를 불러오고 객체가 메모리에 로드되기 때문에 session이 객체보다 먼저 불러와 진 것이다.)
login 직후에 한번

그래서 로그인 시에 에러가 발생한 것이었다.

index.php에 있는 session_start를 지우니 잘 동작한다.

해결

session_start()가 다른 class object보다 먼저 불러와지는지 확인해보고 페이지를 다 로드한 후에 session_start()가 불러와지도록 수정한다.

결론

  1. 템플릿 파일들이 로드 된 후에 session이 동작하도록 해야한다.
  2. session_start()는 serialization/unserialization을 자동적으로 수행하게 한다.
profile
개발자라고 우기는 노답 소년

0개의 댓글