Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 30, 2023 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/1cc14cff7cdcf964ceb4ddeb4478647a to your computer and use it in GitHub Desktop.
Save code-boxx/1cc14cff7cdcf964ceb4ddeb4478647a to your computer and use it in GitHub Desktop.
PHP Callback Examples

PHP CALLBACK EXAMPLES

https://code-boxx.com/php-callback-functions/

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) FUNCTION TO CALLBACK
function funky () { echo "YO!"; }
// (B) PASS FUNKY() INTO CALL_USER_FUNC()
call_user_func("funky");
<?php
// (A) FUNCTIONS TO CALL BACK
function funkyA () { echo "YO!"; }
function funkyB () { echo "MAN!"; }
// (B) THE "MAIN" FUNCTION
function getFunky ($callback) {
if (is_callable($callback)) { $callback(); }
}
// (C) PASS CALLBACK FUNCTIONS INTO "MAIN FUNCTION"
getFunky("funkyA");
getFunky("funkyB");
<?php
// (A) "MAIN FUNCTION"
function repeater ($num, $callback) {
return $callback($num);
}
// (B) CALLBACK FUNCTIONS
function twice ($num) { return $num * 2; }
function thrice ($num) { return $num * 3; }
// (C) RUN!
echo repeater(12, "twice"); // 24
echo repeater(12, "thrice"); // 36
<?php
// (A) "MAIN" FUNCTION - CURL FETCH
function fetch ($url, $after) {
// (A1) FETCH CONTENT FROM URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// (A2) CALLBACK AFTER CURL FETCH
if (is_callable($after)) { $after($result); }
}
// (B) CALLBACK FUNCTIONS
function save ($data) { file_put_contents("demo.html", $data); }
function show ($data) { echo $data; }
// (C) GO!
fetch("https://en.wikipedia.org/wiki/Rule-based_system", "save");
fetch("https://code-boxx.com", "show");
<?php
// (A) CLASS - STATIC FUNCTION TO CALLBACK
class MyClass {
public static function MyFunc () { echo "FOO!"; }
}
// (B) THE "MAIN" FUNCTION
function funky ($callback) {
if (is_callable($callback)) { $callback(); }
}
// (C) PASS STATIC FUNCTION MYFUNC() INTO FUNKY()
funky("MyClass::MyFunc");
<?php
// (A) OBJECT - TO CALLBACK MYFUNC()
class MyClass {
public function MyFunc () { echo "FOO!"; }
}
$myObj = new MyClass();
// (B) THE "MAIN" FUNCTION
function funky ($callback) {
// (B1) FUNCTION IN OBJECT
if (is_array($callback)) { call_user_func($callback); }
// (B2) "REGULAR" CALLBACK FUNCTIONS
else if (is_callable($callback)) { $callback(); }
}
// (C) PASS OBJECT + FUNCTION TO CALL
// SINCE OBJECTS ARE NOT GLOBAL, WE HAVE TO PASS IN ENTIRE OBJECT
funky([$myObj, "MyFunc"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment