これは普通の挙動? なんか違和感ありありなんですけど。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

class Testphp{
  public function echos($str){
    $this->$str = $str;
    return $this->$str;
  }
}

$test1 = new Testphp();
echo $test1->echos("hoge<br />\r\n");
$test2 = new Testphp();
echo $test2->echos("ponta<br />\r\n");
echo "  ----------<br />\r\n";

$moji1 = "hoge<br />\r\n";
$moji2 = "ponta<br />\r\n";
$moji3 = "var<br />\r\n";

echo $test1->echos("$moji2");
echo $test2->echos("$moji1");
echo $test2->$moji2;
echo $test2->$moji3;

?>
1
2
3
4
5
6
7
8
9
$ php testphp.php 
hoge<br />
ponta<br />
  ----------<br />
ponta<br />
hoge<br />
ponta<br />
PHP Notice:  Undefined property: Testphp::$var<br />
 in /path/to/testphp.php on line 24