mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Add JsonSerializable::jsonSerialize() examples.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@326957 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
83c2e760cc
commit
3342a471af
1 changed files with 133 additions and 5 deletions
|
@ -14,11 +14,9 @@
|
|||
<void />
|
||||
</methodsynopsis>
|
||||
<para>
|
||||
|
||||
Serializes the object to a value that can be serialized natively by
|
||||
<function>json_encode</function>.
|
||||
</para>
|
||||
|
||||
&warn.undocumented.func;
|
||||
|
||||
</refsect1>
|
||||
|
||||
<refsect1 role="parameters">
|
||||
|
@ -29,11 +27,141 @@
|
|||
<refsect1 role="returnvalues">
|
||||
&reftitle.returnvalues;
|
||||
<para>
|
||||
Return data which should be serialized by <function>json_encode</function>.
|
||||
Returns data which can be serialized by <function>json_encode</function>,
|
||||
which is a value of any type other than a <type>resource</type>.
|
||||
</para>
|
||||
</refsect1>
|
||||
|
||||
<refsect1 role="examples">
|
||||
&reftitle.examples;
|
||||
<para>
|
||||
<example>
|
||||
<title>
|
||||
<methodname>JsonSerializable::jsonSerialize</methodname> example
|
||||
returning an <type>array</type>
|
||||
</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class ArrayValue implements JsonSerializable {
|
||||
public function __construct(array $array) {
|
||||
$this->array = $array;
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return $this->array;
|
||||
}
|
||||
}
|
||||
|
||||
$array = [1, 2, 3];
|
||||
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
[
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
<example>
|
||||
<title>
|
||||
<methodname>JsonSerializable::jsonSerialize</methodname> example
|
||||
returning an associative <type>array</type>
|
||||
</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class ArrayValue implements JsonSerializable {
|
||||
public function __construct(array $array) {
|
||||
$this->array = $array;
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return $this->array;
|
||||
}
|
||||
}
|
||||
|
||||
$array = ['foo' => 'bar', 'quux' => 'baz'];
|
||||
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
{
|
||||
"foo": "bar",
|
||||
"quux": "baz"
|
||||
}
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
<example>
|
||||
<title>
|
||||
<methodname>JsonSerializable::jsonSerialize</methodname> example
|
||||
returning an <type>integer</type>
|
||||
</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class IntegerValue implements JsonSerializable {
|
||||
public function __construct($number) {
|
||||
$this->number = (integer) $number;
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return $this->number;
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
1
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
<example>
|
||||
<title>
|
||||
<methodname>JsonSerializable::jsonSerialize</methodname> example
|
||||
returning an <type>integer</type>
|
||||
</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class StringValue implements JsonSerializable {
|
||||
public function __construct($string) {
|
||||
$this->string = (string) $string;
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return $this->string;
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode(new StringValue('Hello!'), JSON_PRETTY_PRINT);
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
"Hello!"
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
</refsect1>
|
||||
</refentry>
|
||||
|
||||
<!-- Keep this comment at the end of the file
|
||||
|
|
Loading…
Reference in a new issue