JackDavis
JackDavis

Reputation: 127

PHP: Object-oriented code - Class 'x' not found, but there is

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).

Upvotes: 0

Views: 151

Answers (1)

Christian Gollhardt
Christian Gollhardt

Reputation: 17004

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;

Upvotes: 3

Related Questions