PHP 8.5.0 Alpha 4 available for testing

Voting

: max(zero, zero)?
(Example: nine)

The Note You're Voting On

sebastian dot rapetti at alice dot it
8 years ago
PDOStatement::fetchObject() injects values as string, I needed a conversion type.

I did it including settype() function in class constructor

Below method that find an user by id and return user object instance:

<?php
class UserMapper extends MapperAbstract
{
//other code
public function findById(int $userId)
{
$pdos = $this->dBase->prepare('SELECT user_id AS objectId, name, description, password, active, created,
last_update AS lastUpdate FROM user WHERE user_id = :id'
);

$pdos->bindParam(':id', $userId, \PDO::PARAM_INT);
$pdos->execute();

return
$pdos->fetchObject('\DomainObjects\User', array($this->password));
}
//other code
}
?>

User class with type handling:

<?php
class User extends DomainObjectAbstract
{
//other code
public function __construct(Password $password)
{
$this->passwordUtility = $password;

settype($this->objectId, 'integer');
settype($this->active, 'integer');
}
//other code
}
?>

var_dump() of returned User instance:

<?php
object
(DomainObjects\User)[18]
public
'name' => string 'root' (length=4)
public
'description' => string 'System User' (length=11)
public
'password' => string '$2y$11$4IAn6SRaB0osPz8afZC5D.CmTrBGxnb5FQEygPjDirK9SWE/u8YuO' (length=60)
public
'active' => int 1
public 'created' => string '2015-02-14 10:39:00' (length=19)
public
'lastUpdate' => string '2016-08-30 18:46:56' (length=19)
private
'passwordUtility' =>
object(Auth\Password)[13]
protected
'options' =>
array (
size=1)
'cost' => int 11
protected 'objectId' => int 1
?>

'objectId' and 'active' are now of the type required

<< Back to user notes page

To Top