在PHP类

时间:2015-08-12 15:14:04

标签: php function class

我有以下凯撒密码任务。

class Caesar
{
    protected $secretMessage = 'calvin';
    protected $splitUpMessage;
    protected $splitUpText;
    protected $alphabet=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    private $key = 5;
    protected $encryptedText;

    public function encrypt()
    {
        $alphabet = $this->alphabet;
        $secretMessage = $this->secretMessage;
        $splitUpMessage = $this->splitUpMessage;
        $key = $this->key;

        $splitUpMessage = str_split($secretMessage);
        $flip = array_flip($alphabet);
        $messageCount = strlen($secretMessage);

        foreach ($splitUpMessage as $singleChar) {
            $encryptedText = $alphabet[($flip[$singleChar]+$key)%26];
            print_r($encryptedText);
        }

        echo PHP_EOL;
    }

    public function decrypt()
    {
        $alphabet = $this->alphabet;
        $encryptedText = $this->encryptedText;
        $splitUpText =  $this->splitUpText;
        $key = $this->key;

        $splitUpText = str_split($encryptedText);
        $keyFlip = array_flip($alphabet);
        $charCount = strlen($encryptedText);
    }
}

$encrypt = new Caesar();
// $encrypt->encrypt();
$encrypt->decrypt();

基本上我想做的就是能够在我的$encryptedText函数中调用decrypt。我不知道怎么做。在全球范围内? PHP noob在这里。

感谢您提供任何帮助/建议。

2 个答案:

答案 0 :(得分:1)

将其存回对象:

public function encrypt() {
    ... blah blah blah
    $this->encryptedtext = $temporary_working_local_copy;
}

答案 1 :(得分:1)

用两个函数中的$ this-> encryptedText替换所有$ encryptedText。