php 在同一个控制器中调用其他函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17861412/
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
Calling other function in the same controller?
提问by Himmators
I've this controller, and the function read($q)
return error Call to undefined function sendRequest()
我有这个控制器,还有function read($q)
返回错误Call to undefined function sendRequest()
<?php
class InstagramController extends BaseController {
/*
|--------------------------------------------------------------------------
| Default Home Controller
|--------------------------------------------------------------------------
|
| You may wish to use controllers instead of, or in addition to, Closure
| based routes. That's great! Here is an example controller method to
| get you started. To route to this controller, just add the route:
|
| Route::get('/', 'HomeController@showWelcome');
|
*/
public function read($q)
{
$client_id = 'ea7bee895ef34ed08eacad639f515897';
$uri = 'https://api.instagram.com/v1/tags/'.$q.'/media/recent?client_id='.$client_id;
return sendRequest($uri);
}
public function sendRequest($uri){
$curl = curl_init($uri);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}
I'm assuming it's because I'm referencing the function in the wrong manner, but I can't find any explanations for how to do it.
我假设这是因为我以错误的方式引用了该函数,但我找不到有关如何执行此操作的任何解释。
回答by haim770
Try:
尝试:
return $this->sendRequest($uri);
Since PHP is not a pure Object-Orieneted language, it interprets sendRequest()
as an attempt to invoke a globally defined function (just like nl2br()
for example), but since your function is part of a class ('InstagramController'), you need to use $this
to point the interpreter in the right direction.
由于 PHP 不是纯面向对象的语言,它解释sendRequest()
为尝试调用全局定义的函数(就像nl2br()
example),但由于您的函数是类 ('InstagramController') 的一部分,您需要使用$this
指向正确方向的口译员。
回答by QArea
Yes. Problem is in wrong notation. Use:
是的。问题是错误的符号。用:
$this->sendRequest($uri)
Instead. Or
反而。或者
self::staticMethod()
for static methods. Also read this for getting idea of OOP - http://www.php.net/manual/en/language.oop5.basic.php
对于静态方法。另请阅读本文以了解 OOP - http://www.php.net/manual/en/language.oop5.basic.php