Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:15
Show Gist options
  • Save code-boxx/ca8b0c486dd3b7eefef252baec228c8f to your computer and use it in GitHub Desktop.
Save code-boxx/ca8b0c486dd3b7eefef252baec228c8f to your computer and use it in GitHub Desktop.
PHP Hooks

SIMPLE HOOKS EXAMPLES IN PHP

https://code-boxx.com/php-hooks-simple/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) SAVE USER FUNCTION
$error = ""; // ERROR MESSAGE
function save ($name, $email) {
// (A1) BEFORE HOOK
$pass = true;
if (function_exists("before")) { $pass = before($name, $email); }
// (A2) SAVE USER - THIS EXAMPLE SAVES TO A DUMMY FILE
if ($pass) { if (file_put_contents("user.txt", "$name\r\n$email") === false) {
$GLOBALS["error"] = "Error writing to file.";
$pass = false;
}}
// (A3) AFTER HOOK
if ($pass && function_exists("after")) { $pass = after($name, $email); }
// (A4) RESULT
return $pass;
}
// (B) THE HOOKS
// (B1) BEFORE SAVE - CHECK USER
function before ($name, $email) {
// NAME MUST BE AT LEAST 2 CHARACTERS
if (strlen($name) < 2) {
$GLOBALS["error"] = "Name must be at least 2 characters";
return false;
}
// VALID EMAIL
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$GLOBALS["error"] = "Invalid email";
return false;
}
return true;
}
// (B2) AFTER SAVE - SEND EMAIL
function after ($name, $email) {
if (!mail($email, "Welcome", "Welcome to Test Co.")) {
$GLOBALS["error"] = "Failed to send email to $email";
return false;
}
return true;
}
// (C) GO!
echo save ("Jon Doe", "jon@doe.com") ? "OK" : $error ;
<?php
// (A) SAVE USER FUNCTION
$error = ""; // ERROR MESSAGE
function save ($name, $email, $before=null, $after=null) {
// (A1) BEFORE HOOK
$pass = true;
if ($before!=null) { $pass = $before($name, $email); }
// (A2) SAVE USER
if ($pass) { if (file_put_contents("user.txt", "$name\r\n$email") === false) {
$GLOBALS["error"] = "Error writing to file.";
$pass = false;
}}
// (A3) AFTER HOOK
if ($after!=null) { $pass = $after($name, $email); }
// (A4) RESULT
return $pass;
}
// (B) GO!
echo save (
// (B1) NAME & EMAIL
"Jon Doe", "jon@doe.com",
// (B2) BEFORE
function ($name, $email) {
// NAME MUST BE AT LEAST 2 CHARACTERS
if (strlen($name) < 2) {
$GLOBALS["error"] = "Name must be at least 2 characters";
return false;
}
// VALID EMAIL
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$GLOBALS["error"] = "Invalid email";
return false;
}
return true;
},
// (B3) AFTER
function ($name, $email) {
if (!mail($email, "Welcome", "Welcome to Test Co.")) {
$GLOBALS["error"] = "Failed to send email to $email";
return false;
}
return true;
}
) ? "OK" : $error ;
<?php
// (A) SAVE USER FUNCTION
$error = ""; // ERROR MESSAGE
function save ($name, $email, $before="", $after="") {
// (A1) BEFORE HOOK
$pass = true;
if ($before!="" && file_exists($before)) { include $before; }
// (A2) SAVE USER
if ($pass) { if (file_put_contents("user.txt", "$name\r\n$email") === false) {
$GLOBALS["error"] = "Error writing to file.";
$pass = false;
}}
// (A3) AFTER HOOK
if ($after!="" && file_exists($after)) { include $after; }
// (A4) RESULT
return $pass;
}
// (B) GO
echo save ("Jon Doe", "jon@doe.com", "3b-before.php", "3c-after.php")
? "OK" : $error ;
<?php
// (C) NAME MUST BE AT LEAST 2 CHARACTERS
if (strlen($name) < 2) {
$GLOBALS["error"] = "Name must be at least 2 characters";
$pass = false;
}
// (D) VALID EMAIL
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$GLOBALS["error"] = "Invalid email";
$pass = false;
}
<?php
// (E) SEND EMAIL TO USER
if (!mail($email, "Welcome", "Welcome to Test Co.")) {
$GLOBALS["error"] = "Failed to send email to $email";
$pass = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment