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