たとえば、クラスにこれがあります:
private $messages = array(
'name'=> '<aa_f>name',
'PASS' => '<aa_p>'
);
<aa_p>
しかし、私はこのような変数を作りたい:
private $pass = '<aa_p>';
私はこれらの4つの方法を試しましたが、どれもうまくいきません。PHP.net には、これを行う例がありませんでした。
'PASS' => $this->pass
'PASS' => $pass
'PASS' => '$this->pass'
'PASS' => '$pass'
完全なコード
<?php
class Message
{
private $PASS = '<aa_p>';
private $FAIL = '<aa_f>';
private $messages = array(
'name'=> '<aa_f>name',
'email' => '<aa_f>email_s',
'pass' => '<aa_f>pass',
'url' => '<aa_f>url',
'title' => '<aa_f>title',
'tweet'=> '<aa_f>tweet',
'empty' => '<aa_f>empty',
'same' => '<aa_f>same',
'taken' => '<aa_f>taken',
'validate' => '<aa_f>validate',
'PASS' => '<aa_p>'
);
public function __construct()
{
}
public function display($type)
{
echo $this->messages[$type];
}
public function get($type)
{
return $this->messages[$type];
}
}
更新:回答
参照用: ( 更新されたコード )
class Message
{
private $PASS = '<aa_p';
private $FAIL = '<aa_f>';
private $messages = array();
public function __construct()
{
$this->messages['PASS'] = $this->PASS;
$this->messages['name'] = $this->FAIL . 'name';
$this->messages['email'] = $this->FAIL . 'email_s';
$this->messages['pass'] = $this->FAIL . 'pass';
$this->messages['url'] = $this->FAIL . 'url';
$this->messages['title'] = $this->FAIL . 'title';
$this->messages['tweet'] = $this->FAIL . 'tweet';
$this->messages['empty'] = $this->FAIL . 'empty';
$this->messages['same'] = $this->FAIL . 'same';
$this->messages['taken'] = $this->FAIL . 'taken';
$this->messages['validate'] = $this->FAIL . 'validate';
}
public function display($type)
{
echo $this->messages[$type];
}
public function get($type)
{
return $this->messages[$type];
}
}