Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created September 27, 2018 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeschinkel/908433e2423e4e2cea3c78fc78cfa721 to your computer and use it in GitHub Desktop.
Save mikeschinkel/908433e2423e4e2cea3c78fc78cfa721 to your computer and use it in GitHub Desktop.
Proposed __typeError() magic method
<?php
declare(strict_types=1);
class Product {
public bool $archived;
function __typeError(TypeError $te) {
if ( 'archived' === $te->getVar() ) {
$message = 'TypeError: Line %d in file %s assigns type %s to the `archived` property which should only be assigned a `bool`.';
$message = sprintf( $message, $te->getLine(), $te->getFile(), gettype($te->getValue()));
error_log( $message );
$this->archived = (bool)$this->getValue();
return false;
}
return true;
}
}
$mysqli = new mysqli("example.com", "user", "password", "database");
$result = $mysqli->query( 'SELECT archived FROM table WHERE ID=12345' );
$result->data_seek(0);
$row = $result->fetch_assoc();
$Product = new Product();
$Product->archived = $row['archived'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment