Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php调用函数中的另一个函数_Php_Function - Fatal编程技术网

php调用函数中的另一个函数

php调用函数中的另一个函数,php,function,Php,Function,我试图在另一个函数中调用一个函数。根据一些研究,他们说使用 $this-> 应该有用。但它给了我 致命错误:不在对象上下文中使用$this 要查看我的代码的其余部分,请执行以下操作: 错误: 致命错误:在第48行BLA.php的对象上下文中不使用$this 如果您在类中,则需要$this->,如果不在类中,则只需按函数名调用函数即可: function test($astring) { $output2 = addstring($astring, 'asd', 1); } 除了尼古拉

我试图在另一个函数中调用一个函数。根据一些研究,他们说使用

$this->

应该有用。但它给了我

致命错误:不在对象上下文中使用$this

要查看我的代码的其余部分,请执行以下操作:

错误:

致命错误:在第48行BLA.php的对象上下文中不使用$this


如果您在类中,则需要$this->,如果不在类中,则只需按函数名调用函数即可:

function test($astring) {
    $output2 = addstring($astring, 'asd', 1);
}

除了尼古拉斯提到的错误

function test($astring) {
没有返回值,并且不通过引用使用参数,这意味着函数除了浪费性能之外什么也不做

要演示如何将函数带入
类上下文

class StringHelper
{
    private $output;

    protected function addstring($input, $addition_string , $position) {
        $output = substr_replace($input, $addition_string, $position, 0);
        return $output;
    }

    public function test($astring) {
        $this->output = $this->addstring($astring, 'asd', 1);
        return $this;
    }

    public function getOutput() {
        return $this->output;
    }
}


$stringObj = new StringHelper;
echo $stringObj->test('my string')->getOutput();

“要查看我的其余代码”-您有足够的代表点数发布它。只需删除
$this->
,或者添加一个类即可。
$this->
表示当前类
class StringHelper
{
    private $output;

    protected function addstring($input, $addition_string , $position) {
        $output = substr_replace($input, $addition_string, $position, 0);
        return $output;
    }

    public function test($astring) {
        $this->output = $this->addstring($astring, 'asd', 1);
        return $this;
    }

    public function getOutput() {
        return $this->output;
    }
}


$stringObj = new StringHelper;
echo $stringObj->test('my string')->getOutput();