Php函数返回错误

Php函数返回错误,php,function,Php,Function,我的php函数返回一个错误 这两个函数来自同一个类 错误 致命错误:在D:\xampp\htdocs\admin\functions.php(98):运行时在第1行创建的函数中不在对象上下文中时使用$this public function noFollowLinks($str) { // replaces every link with the version provided by fixLink() return preg_replace_callback("#(<a

我的php函数返回一个错误

这两个函数来自同一个类

错误 致命错误:在D:\xampp\htdocs\admin\functions.php(98):运行时在第1行创建的函数中不在对象上下文中时使用$this

public function noFollowLinks($str) {
    // replaces every link with the version provided by fixLink()
    return preg_replace_callback("#(<a.*?>)#i", create_function('$matches', 'return $this->fixLink($matches[1]);'), $str);
}

public function fixLink($input) {
    $whitelist = $GLOBALS['whitelist'];
    // if the link in $input already contains ref=”nofollow”, return it as it is
    if (preg_match('#rel\s*?=\s*?[\'"]?.*?nofollow.*?[\'"]?#i', $input)) {
        return $input;
    }
    // extract the URL from $input
    preg_match('#href\s*?=\s*?[\'"]?([^\'"]*)[\'"]?#i', $input, $captures);
    // $href will contain the extracted URL, such as http://seophp.example.com
    $href = $captures[1];
    // if URL doesn’t contain http://, assume it’s a local link
    if (!preg_match('#^\s*http://#', $href)) {
        return $input;
    }
    // extract the host name of the URL, such as seophp.example.com
    $parsed = parse_url($href);
    $host = $parsed['host'];
    // if the URL is in the whitelist, send $input back as it is
    if (in_array($host, $whitelist)) {
        return $input;
    }
    // assuming the URL already has a rel attribute, change its value to nofollow
    $x = preg_replace('#(rel\s*=\s*([\'"]?))((?(3)[^\'"]*|[^\'"]*))([\'"]?)#i', '\\1\\3,nofollow\\4', $input);
    // if the string has been modified, it means it already had a rel attribute,
    // whose value has been changed to nofollow, so we return the new version
    if ($x != $input) {
        return $x;
    }
    // if the link in the input string doesn’t have ref attribute, we add it
    else {
        return preg_replace('#<a#i', '<a rel="nofollow"', $input);
    }
}
公共函数noFollowLinks($str){
//用fixLink()提供的版本替换每个链接
return preg_replace_回调(“#()#i”,create_函数(“$matches”,“return$this->fixLink($matches[1])”),$str);
}
公共功能固定链接($input){
$whitelist=$GLOBALS['whitelist'];
//如果$input中的链接已包含ref=“nofollow”,请按原样返回
if(preg#u match('#rel\s*?=\s*?[\'”]?*?nofollow.*?[\'”]?#i',$input)){
返回$input;
}
//从$input中提取URL
preg#u match(“#href\s*?=\s*?[\'”]?([^\'“]*)[\'”]?\i',$input,$CAPTACTS);
//$href将包含提取的URL,例如http://seophp.example.com
$href=$captures[1];
//如果URL不包含http://,则假定它是本地链接
如果(!preg_match('#^\s*http://,$href)){
返回$input;
}
//提取URL的主机名,例如seophp.example.com
$parsed=parse_url($href);
$host=$parsed['host'];
//如果URL在白名单中,请按原样发送$input
if(在_数组中($host,$whitelist)){
返回$input;
}
//假设URL已经有rel属性,将其值更改为nofollow
$x=preg\u replace(“#”(rel\s*=\s*([\'”)(((?(3)[^\'”]*.[^\'”])([\'”]?)\i',“\\1\\3,nofollow\\4',$input);
//如果字符串已被修改,则表示它已具有rel属性,
//其值已更改为nofollow,因此我们返回新版本
如果($x!=$input){
返回$x;
}
//如果输入字符串中的链接没有ref属性,我们将添加它
否则{

返回preg_replace(“#您不能在闭包中使用
$this
,因为它不是对象


你必须这样做:

<?php

       class A {

              public $name ;

              public function doSomething ( Closure $closure ) {
                     return call_user_func_array ( $closure , array ( $this ) ) ;
              }

       }

       $A = new A ( ) ;
       $A->name = 'test' ;
       $A->doSomething(function($object){
              print_r ( $object ) ;
       });

使用闭包时,它作为类外的一个单独函数。它不像其他方法(类内的函数)那样附加到类是,所以使用$this会导致错误,就像您在类之外使用$this一样,正如Andrey所说,您不能在由
create\u function
创建的函数中使用
$this
。 我的想法是用以下内容替换您的
noFollowLinks

public function noFollowLinks($str) {
    // replaces every link with the version provided by fixLink()
    return preg_replace_callback("#(<a.*?>)#i", array($this, 'fixLinkCallback'), $str);
}

private function fixLinkCallback($matches) {
    return $this->fixLink($matches[1]);
}
公共函数noFollowLinks($str){
//用fixLink()提供的版本替换每个链接
返回preg_replace_回调(“#()#i”,数组($this,'fixLinkCallback'),$str);
}
私有函数fixLinkCallback($matches){
返回$this->fixLink($matches[1]);
}

你真的试过自己找出问题所在吗?你的代码让我很头疼。为什么你会包含这么多不必要的评论?如果我没有美元,可能会重复。然后我做了什么,请看一个你必须做的例子。