0

I have the following for Caesar Cipher task I'm doing.

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();

So basically what I want to do is be able to call my $encryptedText in my decrypt function. I'm not sure how to do it though. Globally? PHP noob here.

Thanks for any help/advice.

2
  • What do you mean by "call a variable"? Call is usually used with functions or methods. Commented Aug 12, 2015 at 15:17
  • Are you asking how to pass a variable to decrypt()? Commented Aug 12, 2015 at 15:19

2 Answers 2

1

Store it back into the object:

public function encrypt() {
    ... blah blah blah
    $this->encryptedtext = $temporary_working_local_copy;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Replace all $encryptedText with $this->encryptedText in two functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.