Skip to content

Automatic type casting on Row classes #72

@g105b

Description

@g105b

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions