Re: `PDO::FETCH_CONSTRUCTOR` fetch mode proposal

From: Date: Fri, 19 Jan 2024 15:53:24 +0000
Subject: Re: `PDO::FETCH_CONSTRUCTOR` fetch mode proposal
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi Alexander,

I would also suggest supporting readonly classes and creating special attributes to help map data fields to constructor arguments. Something like this: readonly class User {
    public function  __construct(
        #[PDOField('user_id')]
        public string $userId,
        #[PDOField('user_name')]
        public string $userName,
        #[PDOField('user_address')]
        public ?string $userAddress = '', // Optional field with default
value
    );
}
When the constructor is used to create the class, readonly classes should be automatically supported by this new fetch mode right? And indeed, it would be useful to use named parameters if available. I overlooked this possibility in my response to Saki. Kind regards, Frederik van der Els

Thread (9 messages)

« previous php.internals (#122193) next »