|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$db = mysqli_connect("server","user","pass"); Logger::log('debug','database','Connected to database'); $db->query('INSERT INTO parts (part, description) VALUES ('Hammer','Pounds nails')'; Logger::log('debug','database','Insert Hammer into to parts table'); $db->query('INSERT INTO parts (part, description) values ('Drill','Puts holes in wood')'; Logger::log('debug','database','Insert Drill into to parts table'); $db->query('INSERT INTO parts (part, description) values ('Saw','Cuts wood')'; Logger::log('debug','database','Insert Saw into to parts table'); // One thing that sticks out in Listing 13 is how much repeating we are doing. // Every call made to Logger::log() has the same first two arguments. // To solve this, we can push that method call into a closure and make the // calls against that closure, instead. The resulting code is shown below. //Refactored $log = function ($string) { Logger::log('debug','database',$string); }; $db = mysqli_connect("server","user","pass"); $log('Connected to database'); $db->query('INSERT INTO parts (part, description) values ('Hammer','Pounds nails')'; $log('Insert Hammer into to parts table'); $db->query('INSERT INTO parts (part, description) values ('Drill','Puts holes in wood')'; $log('Insert Drill into to parts table'); $db->query('INSERT INTO parts (part, description) values ('Saw','Cuts wood')'; $log('Insert Saw into to parts table'); |
going forward.
Recent Comments