Jump to content

I couldn't run hook codes


adige

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.