本文实例讲述了php中钩子(hook)的原理与简单应用。分享给大家供大家参考,具体如下:

我们先来回顾下原本的开发流程;

  产品汪搞出了一堆需求;

  当用户注册成功后需要发送短信、发送邮件等等;

  然后聪明机智勇敢的程序猿们就一扑而上;

  把这些需求转换成代码扔在 用户注册成功 和 跳转到首页 之间;

  没有什么能够阻挡;充满创造力的猿们;

<"htmlcode">
<"htmlcode">
<"htmlcode">
<"简易后台管理<hr>";
    // 插件管理
    foreach ($pluginList as $k => $v) {
      // 获取配置项
      $config=include './plugin/'.$v.'/config.php';
      $word=$config['status']==1 "./index.php" rel="external nofollow" >'.$word.'</a><br />';
    }
    echo '<hr>';
    // 输出插件内容
    foreach ($pluginList as $k => $v) {
      // 获取配置项
      $config=include './plugin/'.$v.'/config.php';
      if ($config['status']==1) {
        include './plugin/'.$v.'/index.php';
        // 运行插件
        Hook::run($v);
      }
    }
    // 前往网站首页
  }
}
// 插件类
class Hook{
  // 注册添加插件
  public static function add($name,$func){
    $GLOBALS['hookList'][$name][]=$func;
  }
  // 执行插件
  public static function run($name,$params=null){
    foreach ($GLOBALS['hookList'][$name] as $k => $v) {
      call_user_func($v,$params);
    }
  }
}
// 更改插件状态
if (isset($_GET['change'])) {
  // 获取到配置项
  $config=include './plugin/plugin'.substr($_GET['change'],-1).'/config.php';
  // 如果是开启 那就关闭 如果是关闭 则开启
  $config['status']=$config['status']==1 "<".var_export($config,true).';';
  file_put_contents('./plugin/'.$_GET['change'].'/config.php', $str);
  header('Location:./');
}
$test=new Test();
$test->index();

插件配置项代码:

<"_blank" href="//www.jb51.net/Special/43.htm">php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com