One of things I really like about javascript and particularly JQuery is use of callbacks to hook certain events, such as the success or failure of an ajax call.
This method of hooking events can be useful in php. For example you have a function that a connection to a database. By using callback function you can allow people to handle errors or modify the database object on success.
Here is an example of what I mean:
<?php
function db_connect($dsn, $user, $pass, $success = null, $error = null) {
try {
$database = new PDO($dsn, $user, $pass);
if (!is_null($success) && is_callable($success)) {
return $success($database);
} else {
return $database;
}
} catch (PDOException $e) {
if (!is_null($error) && is_callable($error)) {
return $error($e->getMessage);
} else {
return false;
}
}
}
?>
All this function does is creates a new PDO Object. I have 2 callbacks in this function the first one is a success callback which is called if no exception is thrown when creating the new PDO Object. The second callback is a error callback which is called if an exception is thrown when creating the new PDO Object.
The success callback will take the newly created PDO Object as a parameter. You could use the success callback to set options for the new PDO Object.
Your success callback could look something like this.
<?php
function ($database) {
$database->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
return $database;
}
?>
The error callback will take the message from the exception as a parameter.
Your error callback could look something like this.
<?php
function ($errorMsg) {
echo $errorMsg;
}
?>
Time to put it all together.
<?php
function db_connect($dsn, $user, $pass, $success = null, $error = null) {
try {
$database = new PDO($dsn, $user, $pass);
if (!is_null($success) && is_callable($success)) {
return $success($database);
} else {
return $database;
}
} catch (PDOException $e) {
if (!is_null($error) && is_callable($error)) {
return $error($e->getMessage);
} else {
return false;
}
}
}
$dsn = 'mysql:host=localhost;dbname=test;port=3306';
$db = db_connect($dsn, 'user', 'pass',
function ($database) {
$database->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
return $database;
},
function ($errorMsg) {
echo $errorMsg;
}
);
?>
Using the above to defined your callbacks, it can be diffuclt to tell which callback is which. If we modify the db_connect function to take an array that contains our callback functions and modify the function call we get a more JQuery like syntax, like so.
<?php
function db_connect($dsn, $user, $pass, $callbacks = array()) {
try {
$database = new PDO($dsn, $user, $pass);
if (isset($callbacks['success']) && is_callable($callbacks['success'])) {
return $callbacks['success']($database);
} else {
return $database;
}
} catch (PDOException $e) {
if (is_null($callbacks['error']) && is_callable($callbacks['error'])) {
return $callbacks['error']($e->getMessage);
} else {
return false;
}
}
}
$dsn = 'mysql:host=localhost;dbname=test;port=3306';
$db = db_connect($dsn, 'user', 'pass', array(
'success' => function ($database) {
$database->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
return $database;
},
'error' => function ($errorMsg) {
echo $errorMsg;
},
));
?>
Please note that this code has been tested on PHP 5.4 with that being said I'm not sure if the array with the callback functions will work in PHP 5.3 or lower.
Now you can use the power of callbacks and add some flare to your code.