{"id":568,"title":"php\u8bbe\u8ba1\u6a21\u5f0f-\u9002\u914d\u5668\u6a21\u5f0f\u53ca\u5e94\u7528\u573a\u666f\u4e3e\u4f8b","good":0,"bad":0,"hit":1325,"created_at":"2023-01-19 06:58:28","content":"
<?php<\/p>
\/**<\/p>
* \u751f\u6d3b\u4e2d\u7684\u573a\u666f\uff1a\u7535\u6e90\u5145\u7535\u5934\u6709\u4e9b\u7535\u538b\u4e0d\u4e00\u6837\uff08\u5982\u56fd\u5916\u7684\uff09\uff0c\u5728\u4e0a\u9762\u518d\u63a5\u4e00\u4e2a\u56fd\u5185\u7684\u8f6c\u6362\u5934\u5c31\u884c\u4e86<\/p>
* \u9002\u914d\u5668\u6a21\u5f0f,\u4e0d\u6539\u53d8\u539f\u6765\u7684\u7a0b\u5e8f\uff0c\u5728\u5916\u9762\u518d\u5957\u4e00\u5c42\uff0c\u6709\u70b9\u50cf\u5957\u5a03\uff0c\u548c\u88c5\u9970\u6a21\u5f0f\u5f88\u50cf\uff0c\u533a\u522b\u5728\u4e8e\uff1a<\/p>
* \u9002\u914d\u5668\u6a21\u5f0f\u548c\u88c5\u9970\u5668\u6a21\u5f0f\u90fd\u53ef\u4ee5\u5728\u539f\u7c7b\u6587\u4ef6\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\u4fee\u6539\u3001\u589e\u5f3a\uff0c\u4f46\u4fa7\u91cd\u70b9\u4e0d\u4e00\u6837\uff0c<\/p>
* \u9002\u914d\u5668\u6a21\u5f0f\u91cd\u5728\u5c06\u4e00\u4e2a\u63a5\u53e3\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u63a5\u53e3<\/p>
* \u88c5\u9970\u5668\u6a21\u5f0f\u4fa7\u91cd\u5bf9\u539f\u7c7b\u6587\u4ef6\u65b9\u6cd5\u7684\u589e\u5f3a\uff0c\u4f46\u63a5\u53e3\u8fd8\u662f\u540c\u4e00\u4e2a\u63a5\u53e3<\/p>
*\/<\/p>
<\/p>
\/\/\u5b9a\u4e49\u63a5\u53e3<\/p>
interface renderInterface<\/p>
{<\/p>
public function renderData();<\/p>
}<\/p>
<\/p>
\/\/service:\u539f\u6765\u7684\u5b9e\u73b0<\/p>
class service implements renderInterface<\/p>
{<\/p>
private $data;<\/p>
<\/p>
public function __construct(string $data)<\/p>
{<\/p>
$this->data = $data;<\/p>
}<\/p>
<\/p>
public function renderData(): string<\/p>
{<\/p>
return $this->data;<\/p>
}<\/p>
}<\/p>
<\/p>
\/\/\u540e\u6765\u8981\u5c06\u6570\u636e\u8f6c\u6210json\u6216xml\u683c\u5f0f<\/p>
\/\/\u5b9a\u4e49\u4e00\u4e2ajson\u63a5\u53e3<\/p>
interface jsonInterface<\/p>
{<\/p>
public function renderData();<\/p>
}<\/p>
<\/p>
\/\/\u5b9a\u4e49\u4e00\u4e2a\u9002\u914d\u5668\uff1a\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4e2d\u95f4\u5c42\uff0c\u5957\u5a03<\/p>
class jsonAdapter implements jsonInterface<\/p>
{<\/p>
private $service;<\/p>
<\/p>
public function __construct(string $data)<\/p>
{<\/p>
$this->service = new service($data);<\/p>
}<\/p>
<\/p>
public function renderData()<\/p>
{<\/p>
return json_encode(['old' => $this->service->renderData()], JSON_UNESCAPED_UNICODE);<\/p>
}<\/p>
}<\/p>
<\/p>
\/\/\u8c03\u7528<\/p>
$service = new service('\u539f\u59cb\u6570\u636e');<\/p>
echo $service->renderData() . "<br\/>";<\/p>
$jsonService = new jsonAdapter('\u539f\u59cb\u6570\u636e');<\/p>
echo $jsonService->renderData();<\/p>
<\/p>
\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>
\u539f\u59cb\u6570\u636e<\/span> <\/span><\/p> tips:\u5173\u4e8e\u88c5\u9970\u6a21\u5f0f\u8bf7\u770b\u8fd9\u91cc<\/p> php\u8bbe\u8ba1\u6a21\u5f0f-\u88c5\u9970\u5668\u6a21\u5f0f (hu-rong.com)<\/a><\/p>
{"old":"\u539f\u59cb\u6570\u636e"}<\/span><\/p>
<\/span><\/p>
<\/span><\/p>
<\/span>
<\/p>"}