
Serializable 인터페이스의 serialize 메서드는 객체의 상태를 문자열로 변환하는 메서드가 아닙니다. 대신, PHP의 serialize 함수를 사용하여 객체를 문자열로 변환할 수 있습니다.
예를 들어, 다음과 같이 사용할 수 있습니다:
#hostingforum.kr
php
class Person {
public $name;
public $age;
function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$person = new Person('John', 30);
$serializedPerson = serialize($person);
print($serializedPerson);
이 코드는 Person 객체를 문자열로 변환합니다. serialize 함수는 객체의 속성을 포함하여 모든 정보를 문자열로 변환합니다.
만약, 특정 속성을 제외하고 변환하고 싶다면, serialize 함수의 두 번째 인자로 제외할 속성을 지정할 수 있습니다. 예를 들어:
#hostingforum.kr
php
$serializedPerson = serialize($person, ['name']);
이 코드는 Person 객체의 age 속성을 제외하고 변환합니다.
2025-07-21 00:05