php - Laravel Twitter 库 - 静态函数 getUserTimeline() 在哪里

标签 php oop laravel twitter

我是 Laravel 的新手,我正在尝试了解它是如何工作的。我已经按照定义设置了这个库。

https://github.com/thujohn/twitter-l4

当我仅定义 Twitter 的使用时,示例效果非常好;如下图

    use Stream;
    use Twitter;

    class GoController extends \BaseController{
        function go($id){
             return Twitter::getUserTimeline(array('screen_name' => 'thujohn', 'count' => 20, 'format' => 'json'));
        }
}

但是当我使用 Thujohn\Twitter\Twitter 时它会抛出此错误;

    use Stream;
    use Thujohn\Twitter\Twitter;

    class GoController extends \BaseController{
        function go($id){
             return Twitter::getUserTimeline(array('screen_name' => 'thujohn', 'count' => 20, 'format' => 'json'));
        }
}

Non-static method Thujohn\Twitter\Twitter::getUserTimeline() should not be called statically, assuming $this from incompatible context

那么,我无法理解为什么我不能使用第二种形式?

最佳答案

因为在第二个示例中您直接使用该类,而该类恰好与别名访问器具有相同的名称。

你所看到的静态调用是一个Façade,它实际上使用静态方法实例化类(我不太擅长解释......)

如果您按照说明操作,您应该已在 config/app.php 文件中创建了一个别名。

'Twitter' => 'Thujohn\Twitter\TwitterFacade',

这个别名正是您需要调用的 Twitter 类(即外观),并且您在文档中看到了它的调用。

因此,删除 use 语句 use Thujohn\Twitter\Twitter; 并且您的代码将使用“Twitter”别名(就像第一个示例中那样) ,即将调用 Facade 访问器。

 use Stream;

 class GoController extends \BaseController
 {

    public function go($id)
    {
      return \Twitter::getUserTimeline(array('screen_name' => 'thujohn', 'count' => 20, 'format' => 'json'));
    }
 }

关于php - Laravel Twitter 库 - 静态函数 getUserTimeline() 在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27303991/

相关文章:

php - 在产品页面上显示可用库存数量

php - 如何将文本文件插入到数据库表mysql

php - Codeigniter 查询结果返回带有 setter 的自定义结果对象

php - 避开 laravel 的公用文件夹,直接在 web 服务器中打开根目录

PHP从mysql中获取数组中的多个数据

JAVA - POST 到 PHP,不接收变量

oop - Scheme 可以与 Microsoft COM 一起使用吗?

python - 两部分 : Why doesn't this code call the function I want, 我可以在不为其创建单独函数的情况下执行此操作吗?

php - Laravel 不将特定表单数据存储在数据库中而不给出错误

jquery - 通过 ckfinder 上传后在页面上显示图像