Using PHP 5.3.3 and Drupal 7.17 I’ve created a short custom PHP script, in which I’d like to call Drupal’s function – which in turn expects an object having a pass property:
function user_check_password($password, $account) {
if (substr($account->pass, 0, 2) == 'U$') {
$stored_hash = substr($account->pass, 1);
$password = md5($password);
}
.....
}
So in my script I try to create such an object “on the fly”:
<?php
require_once('/path/to/drupal/includes/password.inc');
........
$sth = $db->prepare(SQL);
$sth->execute(array($username));
if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
if (user_check_password($password, array('pass' => $row['pass']))) {
header('Content-Type: application/json; charset=utf-8');
print json_encode($row);
}
}
However this gives me an error: Trying to get property of non-object
Which probably means: PHP doesn’t accept my array, it wants an object there.
How could I create an anonymous object “on the fly” then to make it happy?
You are getting an error, because
Arrayis not anObject. If you don’t want to manually create an object (as in the answer by @adityamenon), then you can simply cast your array to object: