php 使用同一个类中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1938876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:21:54  来源:igfitidea点击:

Using functions from within the same class

phpclassfunction

提问by Ben Shelock

This is probably a really simple question however Google isn't my friend today.

这可能是一个非常简单的问题,但今天谷歌不是我的朋友。

I have something like this but it says call to undefined function

我有类似的东西,但它说调用未定义的函数

<?php
class myClass{
    function doSomething($str){
        //Something is done here
    }
    function doAnother($str){
        return doSomething($str);
    }
}

?>

?>

回答by Konamiman

Try the following:

请尝试以下操作:

return $this->doSomething($str);

回答by Igor Zinov'yev

You can try a static call like this:

您可以尝试这样的静态调用:

function doAnother ($str) {
    return self::doSomething($str);
}

Or if you want to make it a dynamic call, you can use $this keyword, thus calling a function of a class instance:

或者,如果您想使其成为动态调用,则可以使用 $this 关键字,从而调用类实例的函数:

function doAnother ($str) {
    return $this->doSomething($str);
}

回答by Bart Kiers

Try:

尝试:

return $this->doSomething($str);

Have a look at this as well: http://php.net/manual/en/language.oop5.php

也看看这个:http: //php.net/manual/en/language.oop5.php

回答by The.Anti.9

try:

尝试:

return $this->doSomething(str);