Object of class .. could not be converted to string
This error messageâPHP Catchable fatal error: Object of class Cryption could not be converted to stringâis a classic symptom of trying to pass an object instance directly into a function expecting a scalar value. In your specific case, the issue arises in the SQL query construction within your Cryption class usage.
When you execute $password->sha512(), the method returns a string (the hashed result). However, looking at how you utilize this result in your mysql_query call:
$result = mysql_query("SELECT * FROM `administrators` WHERE username='$username' and password='$password'");
You are passing the object itself into the query string placeholder $password. PHP attempts to interpolate this variable directly into the SQL statement. Since $password is an instance of your custom Cryption class, not a raw string or number, PHP throws the fatal error because it cannot implicitly convert an object to a string without an explicit conversion method.
The Immediate Fix: Method Chaining and Return Values
To resolve this, you must ensure that every step of your logic returns a string before it is used in a context requiring one. Specifically, the sha512() method should be called, and its return value assigned to a variable or used immediately in the query.
Here is how that specific line should look:
$passwordHash = $password->sha512();
$result = mysql_query("SELECT * FROM `administrators` WHERE username='$username' and password='$passwordHash'");
By extracting the hash into $passwordHash, you ensure that the variable passed to mysql_query contains the raw string output of the hashing function, satisfying the SQL requirement for a string comparison.
Critical Security Architecture Flaws
While fixing the type conversion error is necessary for the code to run, the underlying architecture of this script presents severe security vulnerabilities that require immediate attention regardless of the fix.
1. Usage of Deprecated and Unsafe Functions
The code relies heavily on functions from the mysql_* extension (e.g., mysql_query, mysql_fetch_row, mysql_real_escape_string). This extension has been deprecated since PHP 5.5 and removed in PHP 7.0. Furthermore, mysql_real_escape_string is notoriously unreliable because it depends entirely on the current connection's character set configuration; if the charset changes between the escape call and the query execution, SQL injection becomes possible.
The modern standard is to use PDO or MySQLi with prepared statements. Prepared statements handle data escaping automatically and safely, rendering manual string interpolation (like username='$username') obsolete and dangerous.
2. Obsolete Cryptography Implementation
The Cryption class relies on mcrypt_encrypt and hardcoded algorithm names like MCRYPT_RIJNDAEL_256. The mcrypt extension was removed from the PHP core in 2014 due to broken implementations and lack of support for modern standards. Additionally, using a static string 'Na' as the key derivation material is insecure; keys must be cryptographically random.
For password hashing, you should never implement custom encryption logic like sha512() combined with arbitrary salts manually. Instead, use the Password Hashing extension (hash_pbkdf2, password_hash, and password_verify). These functions automatically handle salt generation, key stretching, and storage format compliance (Argon2id or bcrypt).
Refactoring Toward Modern Standards
If you are maintaining legacy code for a project hosted on laravelcompany.com or similar platforms, migrating away from these patterns is non-negotiable. Here is the modern approach using PDO and the Password Hashing extension:
$pdo = new PDO($dsn, $user, $pass);
// Use password_hash to store the hash in the database
$hashedPassword = password_hash($_POST['password'], PASSWORD_ARGON2ID);
$stmt = $pdo->prepare("SELECT * FROM administrators WHERE username = :username AND password = :password");
$stmt->execute(['username' => $_POST['username'], 'password' => $hashedPassword]);
$row = $stmt->fetch();
This approach eliminates the need for manual salt management, fixes the "object to string" conversion issue by using native string-returning functions, and secures the application against SQL injection. Always prioritize security primitives provided by the language core over custom implementations found in legacy scripts.