0

I have problem with including CryptoGuard, but maybe there is my issue with object-oriented code, because I am newbie in that.

require_once('CryptoGuard.php'); // = https://github.com/CoreProc/crypto-guard/blob/master/src/CryptoGuard.php

$passphrase = 'my_private_key'; 
$cryptoGuard = new CryptoGuard($passphrase);
$stringToEncrypt = "private string";
$encryptedText = $cryptoGuard->encrypt($stringToEncrypt);
echo $encryptedText;

Easy usage of CryptoGuard example: https://github.com/CoreProc/crypto-guard (same as I used, but I do not use Composer so I just copied CryptoGuard.php).

There is no php error, but the part with cryptoGuard broke page (stop loading anymore things, no $encryptedText echo there).

5
  • Or maybe (for whatever reason) the result is just empty. Can you try echo '>' . $encryptedText . '<'; just to make sure whether or not that statement is executed? Commented Feb 24, 2015 at 16:19
  • Hi MBaas, thanks for your reply. No, the page freeze and do not load anything more. I tested and it load just content before "$cryptoGuard = new CryptoGuard($passphrase);", nothing after that. Commented Feb 24, 2015 at 16:36
  • What is var_dump($cryptoGuard->encrypt($stringToEncrypt));? and is error reporting set to error_reporting(E_ALL);? Commented Feb 24, 2015 at 16:39
  • Hello Christian, thanks for your reply. I set error_reporting(E_ALL); and I see the error: Fatal error: Class 'CryptoGuard' not found in code.php on line 44 (= $cryptoGuard = new CryptoGuard($passphrase);). So it looks that there is problem with object-oriented code as I thinked, but really dont know how to fix that, because require_once works. Commented Feb 24, 2015 at 16:56
  • To get sure if is CryptoGuard.php loaded correctly I added echo "loaded"; at the end of CryptoGuard.php file and it is really loaded correctly. Very strange, where can be problem please? Commented Feb 24, 2015 at 17:13

1 Answer 1

3

Your Problem is the Namespace. CryptoGuard uses Coreproc\CryptoGuard;

So your Code should be

require_once('CryptoGuard.php'); // = https://github.com/CoreProc/crypto-guard/blob/master/src/CryptoGuard.php

$passphrase = 'my_private_key';

//Not missing the Namespace here
$cryptoGuard = new \Coreproc\CryptoGuard\CryptoGuard($passphrase);
$stringToEncrypt = "private string";
$encryptedText = $cryptoGuard->encrypt($stringToEncrypt);
echo $encryptedText;

Alternative, your provided Code will work, if you write at the beginning of your script:

use \Coreproc\CryptoGuard\CryptoGuard;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I implemented object-oriented code first time, so I did not know what does "namespace" do. :)
No problem :) Everyone begins with something. Glad I can help. Namespaces and OOP are extremly powerfull, you should do more often something with it. And with a good IDE such like PHPStrom they make your development much faster.

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.