doupi3874 2014-07-17 12:48
浏览 50

为什么我不能调用包含在标题中的文件中的函数?

I'm using Wordpress with a custom theme.

At the top of the header.php of the theme I've got a Mobile Detect script.

require_once '/extras/Mobile_Detect.php';
$detect = new Mobile_Detect;

Then further down the header.php page I have a line that checks if the user is on a Mobile, and displays different content accordingly.

if ($detect->isMobile()) { echo "Premium"; } else { echo variable('prem_no'); }

However, in a further included PHP file further down the page, when I try to use the same script:

if ($detect->isMobile()) { echo "Find a psychic"; } else { echo "Our Psychic Readers"; }

I get this error:

Fatal error: Call to a member function isMobile() on a non-object in C:\wamp\www\clairvoyant\extraseader-categories.php on line 1

How can I make it so that a file required or included in the header.php file, can be referenced to throughout the rest of the page files?

I'm a bit confused between include, include_once, require and require_once, so can someone help clear that up?

  • 写回答

2条回答 默认 最新

  • duangu6588 2014-07-17 12:50
    关注

    Not too many differences between require and include but the main point is with require the file must exist or you get an error while include is optional.

    and the _once is to make sure you don't include it more than once.


    However, in a further included PHP file further down the page, when I try to use the same script I get this error...

    This sounds like $detect is not the same object for example

    require_once '/extras/Mobile_Detect.php';
    $detect = new Mobile_Detect;
    if ($detect->isMobile()) { echo "Premium"; }//Good
    ....
    $detect['mobile'] = 'iPhone';
    if ($detect->isMobile()) { echo "Find a psychic"; }//Bad
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?