例如,我在课堂上有这个:
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];
}
}