<?php
// Outside of the application, e.g. DB or some storage.
function addSomeIntegerFieldToDB($value)
{
$sql = 'INSERT INTO mytable (integer_field) VALUES (:int)';
// $st = $pdo->prepare($sql);
// $st->execute(array('int' => $value);
echo "executing query: ".str_replace(':int', $value, $sql)." \n";
}
// the application:
global $conditional;
class X {
public function doSmth($id)
{
// Assertion will run independently of $conditional, thus preventing a wrong $id to be passed to ->doSmth() method
assert(is_int($id), new \InvalidArgumentException('wot ar y doin'));
global $conditional;
if ($conditional) {
addSomeIntegerFieldToDB($id);
}
}
}
$conditional = true;
$obj = new X;
$obj->doSmth(13);
$obj->doSmth('x');
$obj->doSmth(13);
executing query: INSERT INTO mytable (integer_field) VALUES (13)
Fatal error: Uncaught InvalidArgumentException: wot ar y doin in /in/Fg7X4:20
Stack trace:
#0 /in/Fg7X4(36): X->doSmth('x')
#1 {main}
thrown in /in/Fg7X4 on line 20
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
executing query: INSERT INTO mytable (integer_field) VALUES (13)
Fatal error: Uncaught InvalidArgumentException: wot ar y doin in /in/Fg7X4:20
Stack trace:
#0 /in/Fg7X4(36): X->doSmth('x')
#1 {main}
thrown in /in/Fg7X4 on line 20
Process exited with code 255.
executing query: INSERT INTO mytable (integer_field) VALUES (13)
Warning: assert(): InvalidArgumentException: wot ar y doin in /in/Fg7X4:20
Stack trace:
#0 /in/Fg7X4(36): X->doSmth('x')
#1 {main} failed in /in/Fg7X4 on line 20
executing query: INSERT INTO mytable (integer_field) VALUES (x)
executing query: INSERT INTO mytable (integer_field) VALUES (13)
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
executing query: INSERT INTO mytable (integer_field) VALUES (13)
Warning: assert(): exception 'InvalidArgumentException' with message 'wot ar y doin' in /in/Fg7X4:20
Stack trace:
#0 /in/Fg7X4(36): X->doSmth('x')
#1 {main} failed in /in/Fg7X4 on line 20
executing query: INSERT INTO mytable (integer_field) VALUES (x)
executing query: INSERT INTO mytable (integer_field) VALUES (13)