Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic type casting on Row classes #72

Closed
g105b opened this issue Oct 30, 2017 · 3 comments
Closed

Automatic type casting on Row classes #72

g105b opened this issue Oct 30, 2017 · 3 comments
Assignees
Labels
Milestone

Comments

@g105b
Copy link
Member

g105b commented Oct 30, 2017

We need to implement a type casting system so that Row property types are casted automatically as they are fetched from the database.

When the typed properties RFC is passed this will be baked into the language, but we can introduce docblock casting already.

There needs to be a way of "registering" classes (or namespaces of classes) for the QueryCollection by name. For example, $db->fetch("customer/getById", 123); will return a Row, but if there is a registered class for "Customer" that extends Row it will return that instead.

Pre property type hints:

class Customer extends Row {
	/** @var int *//
	public $id;
	/** @var DateTime **/
	public $registered_at;
}

Post property type hints:

class Customer extends Row {
	public int $id;
	public DateTime $registered_at;
}

For this functionality to work, the properties must be declared within the class - not as docblock comments to the class. This is for future compatibility when we have typed properties.

Update: This functionality can work with DocBlock comments, until typed properties become available! The Reflection API can look at the doc blocks for properties and cast them there.

@g105b g105b added the feature label Oct 30, 2017
@g105b g105b added this to the v1 milestone Oct 30, 2017
@g105b g105b changed the title Automatic type casting on Row classes fetchAs and fetchAllAs - Automatic type casting on Row classes Oct 30, 2017
@g105b g105b changed the title fetchAs and fetchAllAs - Automatic type casting on Row classes Automatic type casting on Row classes Oct 30, 2017
@g105b g105b self-assigned this Oct 30, 2017
@g105b
Copy link
Member Author

g105b commented Jan 9, 2018

https://3v4l.org/gCCnm a hacky ideafor extracting the fully qualified class name from a comment.

Better answer, with full example: https://stackoverflow.com/a/9896557/431780 - use this to match non-fully-qualified class names within the already-parsed comment.

@g105b g105b modified the milestones: v1, v2 Jul 17, 2018
@g105b
Copy link
Member Author

g105b commented Oct 1, 2018

⚠️ 🚨 THIS IS COMING IN PHP 7.4! ⚠️ 🚨

I'm so excited to see that typed properties RFC v2 has been accepted, and typed properties are finally making their way to the PHP language.

This functionality can be coded without having to force PHP 7.4 as a requirement, because it's up to the developer whether or not they use it.

@g105b
Copy link
Member Author

g105b commented Dec 16, 2020

Done. Oh it's PHP 8 now. I better get a crack on.

@g105b g105b closed this as completed Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant