【问题标题】:Laravel late static binding as static::whereInLaravel 后期静态绑定为 static::whereIn
【发布时间】:2016-11-02 02:56:56
【问题描述】:

好的,我阅读并感觉我对PHP后期静态绑定的方法和变量有所了解。但从 this code on Laravel 5 的第 28 行开始,它与 whereIn 一起使用,这是一个 Laravel 集合方法。我不明白这里发生了什么,@ 987654323@。集合在哪里,以便您可以使用whereIn()

/**
 * Add any tags needed from the list
 *
 * @param array $tags List of tags to check/add
 */
public static function addNeededTags(array $tags)
{
    if (count($tags) === 0) {
        return;
    }

    $found = static::whereIn('tag', $tags)->lists('tag')->all();

    foreach (array_diff($tags, $found) as $tag) {
        static::create([
            'tag' => $tag,
            'title' => $tag,
            'subtitle' => 'Subtitle for '.$tag,
            'page_image' => '',
            'meta_description' => '',
            'reverse_direction' => false,
        ]);
    }
}

【问题讨论】:

  • 这是使用模型中的wherIn(),而不是集合,扩展Illuminate\Database\Eloquent\Model;中的Tag类。
  • 谢谢。很高兴知道。

标签: php laravel late-static-binding


【解决方案1】:

来自php.net的例子:

class a
{
    static protected $test = "class a";

    public function static_test()
    {
        echo static::$test; // Results class b
        echo self::$test; // Results class a
    }
}

class b extends a
{
    static protected $test = "class b";
}

$obj = new b();
$obj->static_test();

所以static::whereIn() 指的是Tag::whereIn()static::create() 也是如此

【讨论】:

  • 另一个有用的link
  • 谢谢。但是您的示例是关于变量的。但是Tag是一个模型。那么它有什么关系呢?
  • 在 laravel 范围内Tag 是一个模型。在 OOP 的范围内Tag 是一个对象。在编程范围内Tag 是语言中的一个专用空间,代表一些概念或包含一个值。变量也是如此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 2011-04-15
  • 2011-02-12
  • 1970-01-01
  • 2012-04-21
相关资源
最近更新 更多