アクセス権限があるプロパティは、foreach文などの反復処理に使用できる。
サンプル
<?php
class sampleClass
{
public $varA = '値A';
public $varB = '値B';
public $varC = '値C';
protected $varD = '値D';
private $varE = '値E';
function sampleMethod() {
echo "<p>sampleClass::sampleMethod</p>";
foreach( $this as $key => $value ) {
print "<p>$key => $value</p>";
}
}
}
$class = new sampleClass();
foreach( $class as $key => $value ) {
print "<p>$key => $value</p>";
}
echo "<br />";
$class->sampleMethod();
?>
class sampleClass
{
public $varA = '値A';
public $varB = '値B';
public $varC = '値C';
protected $varD = '値D';
private $varE = '値E';
function sampleMethod() {
echo "<p>sampleClass::sampleMethod</p>";
foreach( $this as $key => $value ) {
print "<p>$key => $value</p>";
}
}
}
$class = new sampleClass();
foreach( $class as $key => $value ) {
print "<p>$key => $value</p>";
}
echo "<br />";
$class->sampleMethod();
?>
↓↓↓出力結果↓↓↓
varA => 値A
varB => 値B
varC => 値C
sampleClass::sampleMethod
varA => 値A
varB => 値B
varC => 値C
varD => 値D
varE => 値E
varB => 値B
varC => 値C
sampleClass::sampleMethod
varA => 値A
varB => 値B
varC => 値C
varD => 値D
varE => 値E