Skip to content

Instantly share code, notes, and snippets.

@shoaiyb
Last active April 16, 2022 23:59
Show Gist options
  • Save shoaiyb/999b64acc1b62d5af197ea5195ef04cc to your computer and use it in GitHub Desktop.
Save shoaiyb/999b64acc1b62d5af197ea5195ef04cc to your computer and use it in GitHub Desktop.
The simplest PHP Hook

Hook

The simplest PHP Hook System.

How it works

This Hook System store all the hooks and there values in an array.

How can I use

This Hook is the simplest Hook ever made.

How can I set something?

Use the set() member function to set whatever you want, Accept any type mixed.

<?php
// Require the Hook
require 'hook.php';
// Initiate the Hook
$Hook = new Hook();
// Set value `Just For Testing` in the array key `test`
$Hook->set('test', 'Just For Testing');
?>

How can I get something?

Use the get() member function to get value of the provided key.

<?php
// Require the Hook
require 'hook.php';
// Initiate the Hook
$Hook = new Hook();
// Get every value from the key `test`
$Hook->get('test');
?>

How can I delete something?

Use the unset() member function to delete the value of the provided key.

<?php
// Require the Hook
require 'hook.php';
// Initiate the Hook
$Hook = new Hook();
// Delete the value of the key `test`
$Hook->unset('test');
?>

You can make additional changes to this Hook if you like. For anything you didn't understand, Write you comments down ⬇️

<?php
class Hook {
/**
* The hooks container
* @var array $hooks
*/
private $hooks = [];
/**
* Set {@param->$value} inside {@param->$key}
* @param string $key
* @param mixed $value
* @return void
*/
public function set(string $key, mixed $value): void {
$this->hooks[$key][] = $value;
}
/**
* Delete the value's inside {@param->$key}
* @param string $key
* @return void
*/
public function unset(string $key): void {
unset($this->hooks[$key]);
}
/**
* Get the list of all the value's inside {@param->$key}
* @param string $key
* @return string
*/
public function get(string $key): string {
foreach($this->hooks[$key] as $hook) {
echo $hook;
}
}
}
?>
<?php
// Let's get the Hook's core
require 'hook.php';
// Let's initiate Hook
$Hook = new Hook();
// Let's set `Hello World!` inside key `hello`
$Hook->set('hello', 'Hello World!');
// Let's get the value inside key `hello`
$Hook->get('hello');
?>
@shoaiyb
Copy link
Author

shoaiyb commented Sep 29, 2021

Can be used as:

Core File

<?php
require 'hook.php';
$Hook = new Hook();
$Hook->set('head', '<link rel="stylesheet" href="/style.css">');
$Hook->set('body', '<script src="/script.js"></script>');
?>

Index File

<!DOCTYPE html>
<html>
  <head>
    <title>Hook</title>
    <?php $Hook->get('head') ?>
  </head>
  <body>
    <p>Hook</p>
    <?php $Hook->get('body') ?>
  </body>
</html>

Output

<!DOCTYPE html>
<html>
  <head>
    <title>Hook</title>
    <link rel="stylesheet" href="/style.css">
  </head>
  <body>
    <p>Hook</p>
    <script src="/script.js"></script>
  </body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment