Hi everyone,I want to learn PHP hook and I try hook examples.I tired this example but I saw only white page.It has a problem but I can't understand it
Codes;
hook.php
<?php
class Hooks {
public function add($hook,$class=null,$method=null,$args=null) {
if (empty($method)) error("You must include a method (function) when defining add_hook.");
$this->hooks[$hook][]=array((!empty($class)?array($class,$method):$method),$args);
return $this;
}
public function clear($hook=null) { if (!empty($hook)) unset($this->hooks[$hook]); else $this->hooks=null; }
public function run($hook) {
if (empty($this->hooks[$hook])) return $this;
foreach ($this->hooks[$hook] as $hkey=>$hvalue) {
if (is_array($hvalue[0])) $hvalue[0]=array($hvalue[0][0],$hvalue[0][1]);
if (!empty($hvalue[1])) if (!is_array($hvalue[1])) call_user_func($hvalue[0],$hvalue[1]); else call_user_func_array($hvalue[0],$hvalue[1]);
else call_user_func($hvalue[0]);
}
return $this;
}
}
?>
script.php
<?php
// For a function:
function doTitle() { echo $current->title; }
$hooks->add('head_title',null,'doTitle');
// For a class:
class Output {
function body_final() { echo implode("\n",$this->body); }
}
$hooks->add('page_body','Output','body_final');
// To pass multiple arguments into a function, you must use:
function test($arg1,$arg2) { echo "Arg1: {$arg1}, Arg2: {$arg2}"; }
$hooks->add('page_body',null,'test',array('test','ing'));
?>
index.php
<?php
// You must include the hook file here, or you can make an includes file to do all of your startup (as is normally done).
include 'hook.php';
include 'script.php';
?><!DOCTYPE html>
<html>
<head>
<?php run('head_meta'); ?>
<title><?php $hooks->run('head_title'); ?></title>
<?php
$hooks->run('head_links');
$hooks->run('head_scripts');
?>
</head>
<body>
<?php $hooks->run('page_body'); ?>
</body>
</html>
Thank you