您可以使用以下代码在文本文件中写入 php 对象...
$obj = new stdClass();
$obj->name = "My Name";
$obj->birthdate = "YYYY-MM-DD";
$obj->position = "My position";
$objData = serialize( $obj);
$filePath = getcwd().DIRECTORY_SEPARATOR."note".DIRECTORY_SEPARATOR."notice.txt";
if (is_writable($filePath)) {
$fp = fopen($filePath, "w");
fwrite($fp, $objData);
fclose($fp);
}
要读取文本文件以获取您定义的属性...
$filePath = getcwd().DIRECTORY_SEPARATOR."note".DIRECTORY_SEPARATOR."notice.txt";
if (file_exists($filePath)){
$objData = file_get_contents($filePath);
$obj = unserialize($objData);
if (!empty($obj)){
$name = $obj->name;
$birthdate = $obj->birthdate;
$position = $obj->position;
}
}