php 调用未定义函数:Laravel 5.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34443046/
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 23:59:36  来源:igfitidea点击:

Call to undefined function : Laravel 5.1

phplaravel-5laravel-5.1

提问by Tim Lewis

I have a private function as written below in the Controller.

我在控制器中有一个私有函数,如下所示。

private function GetProjects($ProjectStatus) {
    return \App\Models\Project\Project_Model
            ::where('ProjectStatusID', $ProjectStatus)
            ->where('WhoCreatedTheProject', auth()->user()->UserID)->get();
}

Below is the action method that is using this private function.

下面是使用这个私有函数的操作方法。

public function ClientCancelledProjects() {
    $ProjectStatus = \App\Enumeration\Project\ProjectStatus::Cancelled;         
    $MyProjects = GetProjects($ProjectStatus);
    return view("Project.Client.MyProject", array("Projects" => $MyProjects));
}

Below is the Error coming when running the controller.

以下是运行控制器时出现的错误。

Call to undefined function App\Http\Controllers\Project\GetProjects()

调用未定义的函数 App\Http\Controllers\Project\GetProjects()

Somebody knows why this is happening ? I am trying to reuse some lines of code as they are written many times in the Controller.

有人知道为什么会这样吗?我试图重用一些代码行,因为它们在控制器中多次编写。

回答by Tim Lewis

To access functions in a controller from a function in the same controller, use self:::

要从同一控制器中的函数访问控制器中的函数,请使用self::

public function ClientCancelledProjects() {
    $ProjectStatus = \App\Enumeration\Project\ProjectStatus::Cancelled;         
    $MyProjects = self::GetProjects($ProjectStatus);
    return view("Project.Client.MyProject", array("Projects" => $MyProjects));
}

Note: Self::(uppercase) will work depending on the version of phpinstalled, but for older versions, self::is preferred.

注意:(Self::大写)将根据php安装的版本工作,但对于旧版本,self::首选。

Please check this link for more info: PHP - Self vs $this

请查看此链接以获取更多信息:PHP - Self vs $this

回答by aynber

Functions inside of a class are not global functions, and cannot be called that way. You need to use $this->GetProjects()instead.

类内的函数不是全局函数,不能那样调用。你需要$this->GetProjects()改用。