1
0
mirror of https://github.com/php/doc-ja.git synced 2026-03-30 11:02:18 +02:00
Files
archived-doc-ja/reference/classobj/reference.xml
TAKAGI Masahiro fa482f421c sync with en.
git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@195169 c90b9560-bf6c-de11-be94-00142212c4b1
2005-09-05 11:34:14 +00:00

225 lines
5.5 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.11 $ -->
<!-- EN-Revision: 1.15 Maintainer: hirokawa Status: ready -->
<!-- CREDITS: shimooka -->
<!-- Purpose: basic.vartype -->
<!-- Membership: core -->
<reference id="ref.classobj">
<title>クラス/オブジェクト関数</title>
<titleabbrev>クラス/オブジェクト</titleabbrev>
<partintro>
<section id="classobj.intro">
&reftitle.intro;
<para>
以下の関数により、クラスやインスタンスオブジェクトに関する情報を
得ることが可能となります。オブジェクトが属するクラスの名前、その
メンバープロパティ、メソッドを取得可能です。
この関数を使用することにより、オブジェクトのクラスメンバーだけで
なく親クラス(すなわちそのオブジェクトクラスの派生元)の情報を得る
ことも可能です。
</para>
</section>
<section id="classobj.requirements">
&reftitle.required;
&no.requirement;
</section>
<section id="classobj.installation">
&reftitle.install;
&no.install;
</section>
<section id="classobj.configuration">
&reftitle.runtime;
&no.config;
</section>
<section id="classobj.resources">
&reftitle.resources;
&no.resource;
</section>
<section id="classobj.constants">
&reftitle.constants;
&no.constants;
</section>
<section id="classobj.examples">
&reftitle.examples;
<para>
この例では、まず基底クラスおよびそのクラスの派生クラスを定義しま
す。基底クラスは食用か否か、色とかいった、一般的な野菜を記述しま
す。サブクラス<varname>Spinach</varname>はその野菜の料理法と調理
済であるかどうかの情報を追加します。
</para>
<para>
<example>
<title>classes.inc</title>
<programlisting role="php">
<![CDATA[
<?php
// メンバープロパティとメソッドを有する基底クラス
class Vegetable {
var $edible;
var $color;
function Vegetable($edible, $color="green")
{
$this->edible = $edible;
$this->color = $color;
}
function is_edible()
{
return $this->edible;
}
function what_color()
{
return $this->color;
}
} // クラスVegetableの終り
// 基底クラスを拡張する
class Spinach extends Vegetable {
var $cooked = false;
function Spinach()
{
$this->Vegetable(true, "green");
}
function cook_it()
{
$this->cooked = true;
}
function is_cooked()
{
return $this->cooked;
}
} // クラスSpinachの終り
?>
]]>
</programlisting>
</example>
</para>
<para>
続いて、これらのクラスから二つのオブジェクトのインスタンスを作成し、
親クラスを含む情報を出力します。
また、いくつかのユーティリティ関数を定義します。これらは主に変数
を格好良く表示するためのものです。
</para>
<para>
<example>
<title>test_script.php</title>
<programlisting role="php">
<![CDATA[
<pre>
<?php
include "classes.inc";
// ユーティリティ関数
function print_vars($obj)
{
foreach (get_object_vars($obj) as $prop => $val) {
echo "\t$prop = $val\n";
}
}
function print_methods($obj)
{
$arr = get_class_methods(get_class($obj));
foreach ($arr as $method) {
echo "\tfunction $method()\n";
}
}
function class_parentage($obj, $class)
{
if (is_subclass_of($GLOBALS[$obj], $class)) {
echo "Object $obj belongs to class " . get_class($$obj);
echo " a subclass of $class\n";
} else {
echo "Object $obj does not belong to a subclass of $class\n";
}
}
// 二つのオブジェクトのインスタンスを作成
$veggie = new Vegetable(true, "blue");
$leafy = new Spinach();
// オブジェクトに関する情報を出力
echo "veggie: CLASS " . get_class($veggie) . "\n";
echo "leafy: CLASS " . get_class($leafy);
echo ", PARENT " . get_parent_class($leafy) . "\n";
// veggieのプロパティを表示
echo "\nveggie: プロパティ\n";
print_vars($veggie);
// そしてleafyのメソッドを表示
echo "\nleafy: メソッド\n";
print_methods($leafy);
echo "\nParentage:\n";
class_parentage("leafy", "Spinach");
class_parentage("leafy", "Vegetable");
?>
</pre>
]]>
</programlisting>
<para>
注意すべき大事な点ですが、上記の例ではオブジェクト
<varname>$leafy</varname>
<classname>Vegetable</classname>のサブクラスであるクラス
<classname>Spinach</classname>のインスタンスであり、
このスクリプトの最後の部分は以下のような出力となります。
</para>
<screen>
<![CDATA[
[...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable
]]>
</screen>
</example>
</para>
</section>
</partintro>
&reference.classobj.functions;
</reference>
<!-- 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:"../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->