{"id":230780,"date":"2022-12-20T11:01:00","date_gmt":"2022-12-20T08:01:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=230780"},"modified":"2022-12-07T10:10:46","modified_gmt":"2022-12-07T07:10:46","slug":"serializacja-klas-wordpress-z-php","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/pl\/serializacja-klas-wordpress-z-php\/","title":{"rendered":"Serializacja klas WordPress z PHP"},"content":{"rendered":"\n<p>Je\u015bli jeste\u015b przyzwyczajony do pracy z modelami (w dowolnym fundamencie lub frameworku, ale konkretnie WordPress), istnieje szansa, \u017ce \u200b\u200bw pewnym momencie mo\u017ce zaj\u015b\u0107 potrzeba serializacji wyst\u0105pienia modelu.<\/p>\n<p>Jasne, napisanie klasy do bazy danych przy u\u017cyciu wbudowanych funkcji PHP jest do\u015b\u0107 \u0142atwe; jednak wprowadzenie odrobiny elastyczno\u015bci, zw\u0142aszcza w odniesieniu do udost\u0119pniania na innych platformach, jest wa\u017cne.<\/p>\n<p>Za\u0142\u00f3\u017cmy na przyk\u0142ad, \u017ce tworzysz aplikacj\u0119 na WordPressie, kt\u00f3ra b\u0119dzie zawiera\u0142a pewien rodzaj unikalnych informacji reprezentowanych w modelu. Model b\u0119dzie w\u00f3wczas dost\u0119pny za po\u015brednictwem aplikacji mobilnej poprzez <a href=\"https:\/\/developer.wordpress.org\/rest-api\/#why-use-the-wordpress-rest-api\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">REST API<\/a>.<\/p>\n<p>Prawdopodobnie jednym z naj\u0142atwiejszych sposob\u00f3w, aby to zrobi\u0107, jest u\u017cycie JSON. Jest to format, kt\u00f3ry dzia\u0142a w r\u00f3\u017cnych j\u0119zykach i platformach, mo\u017ce by\u0107 \u0142atwo serializowany i deserializowany przez te platformy i przesy\u0142any przez sie\u0107 w razie potrzeby.<\/p>\n<p>Zaimplementowanie tego w PHP jest niezwykle \u0142atwe. Musisz tylko upewni\u0107 si\u0119, \u017ce Twoja klasa implementuje interfejs <a href=\"https:\/\/secure.php.net\/manual\/en\/class.jsonserializable.php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">JsonSerializable<\/a>.<\/p>\n<p><a href=\"https:\/\/wordpress.mediadoma.com\/wp-content\/uploads\/2022\/01\/post-160816-61e71433d1015.png\" data-rel=\"lightbox\"><img decoding=\"async\" class=\"SDStudio-light-box-enable SDStudio-editor-tools-md-imp\" src=\"https:\/\/wordpress.mediadoma.com\/wp-content\/uploads\/2022\/01\/post-160816-61e71433d1015.png\" alt=\"Serializacja klas WordPress z PHP\" ><\/a><\/p>\n<p>Z dokumentacji interfejs wykonuje nast\u0119puj\u0105ce czynno\u015bci:<\/p>\n<blockquote>\n<p>Obiekty implementuj\u0105ce JsonSerializable mog\u0105 dostosowa\u0107 swoj\u0105 reprezentacj\u0119 JSON po zakodowaniu za pomoc\u0105 json_encode().<\/p>\n<\/blockquote>\n<p>Jedyn\u0105 metod\u0105, kt\u00f3r\u0105 klasa musi zapewni\u0107, jest <strong>jsonSerialize<\/strong> i chocia\u017c prawdopodobnie b\u0119dziesz chcia\u0142 serializowa\u0107 wszystkie w\u0142a\u015bciwo\u015bci obiektu (a tak\u017ce jego stan, gdy jest wywo\u0142ywany), mo\u017cesz dostosowa\u0107 implementacj\u0119 w dowolny spos\u00f3b.<\/p>\n<p>Na przyk\u0142ad, je\u015bli masz klas\u0119 i chcesz po prostu zaimplementowa\u0107 jej w\u0142a\u015bciwo\u015b\u0107 name, zaimplementujesz funkcj\u0119 w <a href=\"https:\/\/gist.github.com\/tommcfarlin\/e87e029f8e28ec9681996761f06d7347#file-00-name-property-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">ten<\/a> spos\u00f3b :<\/p>\n<pre><code>&lt;?php\n\nclass Acme implements JsonSerializable \n{\n  \/**\n   * @var string the name of the model as represented on the front-end.\n   *\/\n  protected $name;\n\n  \/\/ More code here.\n\n  \/**\n   * @return string a JSON representation of this class\n   *\/\n  public function jsonSerialize()\n  {\n      return [\n        'name' =&gt; $this-&gt;getName()\n      ];\n  }\n}<\/code><\/pre>\n<p>Je\u015bli chcesz doda\u0107 do tego troch\u0119 wi\u0119cej z\u0142o\u017cono\u015bci, mo\u017cesz <a href=\"https:\/\/gist.github.com\/tommcfarlin\/e87e029f8e28ec9681996761f06d7347#file-01-additional-properties-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">to zrobi\u0107<\/a> :<\/p>\n<pre><code>&lt;?php\n\nclass Acme implements JsonSerializable \n{\n  \/**\n   * @var string the name of the model as represented on the front-end.\n   *\/\n  protected $name;\n\n  \/**\n   * @var array an array of features about this object.\n   *\/\n  protected $features;\n\n  \/\/ More code here.\n\n  \/**\n   * @return string a JSON representation of this class\n   *\/\n  public function jsonSerialize()\n  {\n      return [\n        'name' =&gt; $this-&gt;getName(),\n        'features' =&gt; [\n          'hair' =&gt; $this-&gt;getHairColor(),\n          'eyes' =&gt; $this-&gt;getEyeColor(),\n         ],\n      ];\n  }\n}<\/code><\/pre>\n<p>A je\u015bli chcesz zaj\u0105\u0107 si\u0119 serializacj\u0105 w\u0142a\u015bciwo\u015bci, mo\u017cesz zaimplementowa\u0107 <a href=\"https:\/\/gist.github.com\/tommcfarlin\/e87e029f8e28ec9681996761f06d7347#file-02-acme-serializable-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">p\u0119tl\u0119, aby zbudowa\u0107 tablic\u0119<\/a> :<\/p>\n<pre><code>&lt;?php\n\nclass Acme implements JsonSerializable \n{\n  \/**\n   * @var string the name of the model as represented on the front-end.\n   *\/\n  protected $name;\n\n  \/**\n   * @var array an array of features about this object.\n   *\/\n  protected $features;\n\n  \/\/ More code here.\n\n  \/**\n   * @return string a JSON representation of this class\n   *\/\n  public function jsonSerialize()\n  {\n      $objectArray = [];\n      foreach ($this as $key =&gt; $value) {\n          $objectArray[$key] = $value;\n      }\n\n      return $objectArray;\n  }\n}<\/code><\/pre>\n<p>Niezale\u017cnie od przypadku, gdy wyst\u0105pienie klasy zostanie przekazane do\u00a0 funkcji <strong>json_encode<\/strong>, wywo\u0142a ona\u00a0 funkcj\u0119 <strong>jsonSerialize<\/strong> na obiekcie, aby uzyska\u0107 reprezentacj\u0119 obiektu w zaprojektowanym formacie JSON.<\/p>\n<p>Jest to niezwykle \u0142atwe i pomocne, je\u015bli chodzi o udost\u0119pnianie danych na wielu platformach (lub nawet <a href=\"https:\/\/secure.php.net\/manual\/en\/function.json-decode.php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">na tej samej platformie<\/a> ).<\/p>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">\u0179r\u00f3d\u0142o nagrywania:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/tommcfarlin.com\" class=\"external external_icon\">tommcfarlin.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Korzystaj\u0105c z interfejsu JsonSerializable, dodanie serializacji klas WordPress jest naprawd\u0119 \u0142atwe.<\/p>\n","protected":false},"author":1,"featured_media":236064,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[805],"tags":[1169],"class_list":["post-230780","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-7","tag-affiai-pl"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/posts\/230780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/comments?post=230780"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/posts\/230780\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/media\/236064"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/media?parent=230780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/categories?post=230780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/tags?post=230780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}