出现致命错误:在 Stemmer.php 第 317 行不在对象上下文中时使用 $this
我收到一个致命错误:在 Stemmer.php 第 317 行不在对象上下文中时使用 $this。
目前,我正在使用我在互联网上找到的 Stemmer 类,将单词更改为其词干版本,然后再搜索数据库匹配。
我已阅读了人们遇到类似问题的所有相关帖子。不同之处在于,导致错误的代码肯定是在对象上下文中(下面的代码将显示这一点)。另一个奇怪的事情是,代码的某些部分与之前和之后的错误非常相似,这似乎不会造成任何困难。在不同的时间,错误行已更改为其他一些行。
有谁知道可能导致问题的原因。我正在使用 php5.1.34 如果这有什么区别的话。
调用 Stemmer 类的代码
if (isset($search) && $search != "") {
$filtered_words = WordFilter::filter($search);
foreach($filtered_words as $word) {
if(strlen($word) <= 2) {
continue;
}
$w = Stemmer::stem($word);
$stemmed_words[] = $w;
}
}
Stemmer 类:
class Stemmer
{
...
if ( strlen($word) > 2 ) {
**$word = $this->_step_1($word);**
}
...
}
即使错误发生在代码内的不同位置,也似乎总是有代码尝试调用同一类中的另一个方法。这可能是 php5 中的一个我不知道的错误吗?任何建议将不胜感激。
谢谢 阿奇
I am getting a Fatal error: Using $this when not in object context in Stemmer.php on line 317.
At the moment I am using the Stemmer class which I found on the internet to change words to their stemmed version before searching the database for matches.
I have read all the related posts where people are having a similar problem. The difference is that the code causing the error definitely is within object context (the code below will show that). The other strange thing is that there are parts of the code very similar to the error before and after it which don't seem to cause any difficulties. At different times the error line has changed to some of these other lines.
Does anyone have any ideas what could be causing the problem. I am using php5.1.34 if this makes any difference.
Code which calls the Stemmer class
if (isset($search) && $search != "") {
$filtered_words = WordFilter::filter($search);
foreach($filtered_words as $word) {
if(strlen($word) <= 2) {
continue;
}
$w = Stemmer::stem($word);
$stemmed_words[] = $w;
}
}
Stemmer class:
class Stemmer
{
...
if ( strlen($word) > 2 ) {
**$word = $this->_step_1($word);**
}
...
}
Even when the error occurs in difference places within the code it always seems to be when there is code trying to call another method within the same class. Could this be a bug in php5 that I am not aware of? Any advice would be most appreciated.
Thanks
Archie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在静态方法中使用 $this 。
静态方法没有实例;您必须访问其他静态属性/方法或在静态方法中创建一个实例才能使用。
例如
在类中声明为
Your using $this in a static method.
Static methods don't have an instance; you have to access other static properties/methods or create an instance within the static method to work with.
E.g.
where declared in class as
出现这个错误,是因为
stem
不是静态类,他使用了$this。尝试使用此代码:This error ocurred, because
stem
is not a static class, he uses $this. Try to use this code: