2

例如,我在课堂上有这个:

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];
    }
}
4

3 回答 3

2

如果你的意思是你有这样的东西:

class Test {
    private $pass = '<aa_p>';
    private $messages = array(
        'name' => '...',
        'PASS' => '...'
    );
}

并且您想设置'PASS'为私有$pass属性的值,那么您不能在定义中执行此操作,因为 PHP 语法不允许这样做。您必须在可以评估表达式的地方执行此操作,例如在方法中。构造函数将是一个很好的地方:

class Test {
    private $pass = '<aa_p>';
    private $messages = array(
        'name' => '...'
    );

    public function __construct() {
        $this->messages['PASS'] = $this->pass;
    }
}
于 2012-05-15T19:26:37.797 回答
2

您需要在构造函数中设置变量。在构造函数之外,您无法读取其他变量。

class Message{
    private $PASS = '<aa_p>';
    private $messages = array();

    public function __construct(){
        $this->messages['PASS'] = $this->PASS;
    }
}
于 2012-05-15T19:26:48.733 回答
2

只有可以在编译时确定的值才能用于启动类成员。您需要在运行时设置值(通过构造函数):

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' =>       null
    );


    public function __construct()
    {
        $this->messages['PASS'] = $this->PASS;
    }

}
于 2012-05-15T19:27:24.303 回答