Re: `PDO::FETCH_CONSTRUCTOR` fetch mode proposal

From: Date: Fri, 19 Jan 2024 15:46:53 +0000
Subject: Re: `PDO::FETCH_CONSTRUCTOR` fetch mode proposal
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-122192@lists.php.net to get a copy of this message
Hi Saki,
Welcome to the internal mailing list! I had looked the pull request, and I'm pleased to have received your email promptly.
Thank you!
Now, I agree that it is often not possible to use PDO::FETCH_CLASS because DB column names are typically snake case and variables and properties are camel case. IMHO, passing arguments to the constructor in order is a good approach, but I think another option would be to give attributes to properties and map data appropriately when using PDO::FETCH_CLASS. e.g.
#[ColumnName('user_name')]
private string $userName;
The main motivation for this new fetch mode is the fact that it is now infeasible to use the constructor. You would expect to use PDO::FETCH_CLASS for it, as you're trying to create a class, but that is impossible entirely. You need to resort to a hacky usage of PDO::FETCH_FUNC, which in my view is unwanted. A nice consequence of using the constructor is that the casings need not match. I like the idea of using annotations, but they solve a different problem I think.
In addition to the concerns mentioned in the pull request, I'm also concerned that more modes with similar functionality will confuse users.
That is a valid concern. When I first starting looking into this I was also a bit overwhelmed by the amount of possibilities one has. I think giving an appropriate name to this new fetch mode is therefore important. I thought of PDO::FETCH_CONSTRUCTOR, but I'm happy to hear other ideas. Kind regards, Frederik van der Els

« previous php.internals (#122192) next »