PHP では、クラス内ではプロパティに、いろいろと値を入れるかと思います。そして、メソッドでもプロパティでも、 $this
をつけて、使うと思います。
lang
1<?php 2 3class foobar { 4 private $foo; 5 private $bar; 6 7 public function hoge() { 8 echo $this->foo; 9 } 10}
しかし、引数の値は、 $this->
をつけません。
lang
1 2 public function __construct( $a, $b ) { 3 $this->foo = $a; 4 $this->bar = $b; 5 } 6
ここで質問なのですが、 $this->
を付けない変数は、上の例のような場合の他以外に、一時的に使いたい値を入れておくために用意することはできないのでしょうか?
例えば、(3+5)*(2/4)を、カッコを使わないで、以下のような書き方をすることはできないのでしょうか?
lang
1class calc{ 2 3 public $result; 4 5 public function __construct(){ 6 7 $foo = 3+5; 8 $bar = 2/4; 9 10 $this->result = $foo * $bar; 11 12 } 13 14 public function print(){ 15 echo $this->result; 16 } 17 18}
自分で試せば、わかると思いますが、$this
について、詳しく知りたいので、よろしくお願いします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/02/20 14:03