1
0
mirror of https://github.com/php/doc-ja.git synced 2026-03-24 07:02:08 +01:00
Files
archived-doc-ja/language/types/object.xml
2025-08-28 21:00:44 +09:00

110 lines
3.1 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: e587d0655e426f97b3fcb431453da5030e743b23 Maintainer: takagi Status: ready -->
<!-- CREDITS: hirokawa,shimooka,mumumu -->
<sect1 xml:id="language.types.object">
<title>オブジェクト</title>
<sect2 xml:id="language.types.object.init">
<title>オブジェクトの初期化</title>
<para>
オブジェクトを初期化するためには、<literal>new</literal>
命令によりオブジェクトのインスタンスを変数に作成します。
</para>
<example>
<title>オブジェクトの作成</title>
<programlisting role="php">
<![CDATA[
<?php
class foo
{
function do_foo()
{
echo "foo を実行します。";
}
}
$bar = new foo;
$bar->do_foo();
?>
]]>
</programlisting>
</example>
<simpara>
詳細な事項については、
<link linkend="language.oop5">クラスおよびオブジェクト</link>
のセクションを参照ください。
</simpara>
</sect2>
<sect2 xml:id="language.types.object.casting">
<title>オブジェクトへの変換</title>
<para>
オブジェクトがオブジェクトに変換される場合はなにも修正されません。
オブジェクト以外の型の値がオブジェクトに変換される時には、<classname>stdClass</classname> というビルトインクラス予めPHPの内部で定義されているクラスのインスタンスが新しく生成されます。
その際、値が null の場合は新しいインスタンスは空となります。
また、配列がオブジェクトに変換される場合、配列のキーと値がそれぞれオブジェクトのプロパティ名とその値となります。
PHP 7.2.0
より前のバージョンでは、数値のキーの場合プロパティ名によるアクセスはできなかった点に注意して下さい。
</para>
<example>
<title>オブジェクトにキャストする</title>
<programlisting role="php">
<![CDATA[
<?php
$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)'
// PHP 8.1 以降は非推奨
var_dump(key($obj)); // outputs 'string(1) "1"'
?>
]]>
</programlisting>
</example>
<para>
上記以外の値の場合には、<literal>scalar</literal> という名前のメンバ変数が値を格納します。
</para>
<example>
<title><literal>(object)</literal> によるキャスト</title>
<programlisting role="php">
<![CDATA[
<?php
$obj = (object) 'ciao';
echo $obj->scalar; // 'ciao' を出力します
?>
]]>
</programlisting>
</example>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->