Created
September 27, 2018 09:00
-
-
Save mikeschinkel/908433e2423e4e2cea3c78fc78cfa721 to your computer and use it in GitHub Desktop.
Proposed __typeError() magic method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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