2

Ok, I read and feel I have some understandings about PHP late static binding for methods and variables. But from line 28 in this code on Laravel 5, it uses with whereIn which is a Laravel Collection method. I don't understand what's going on here, static::whereIn(). Where is the collection so that you can use 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,
        ]);
    }
}
shin
  • 31,901
  • 69
  • 184
  • 271

1 Answers1

1

An example from 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();

So static::whereIn() refers to Tag::whereIn(). Same goes for static::create()

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
  • 1
    Another useful [link](http://stackoverflow.com/questions/11710099/what-is-the-difference-between-selfbar-and-staticbar-in-php). – Thomas Van der Veen Jun 30 '16 at 07:09
  • Thanks. But your example is about variables. But Tag is a model. So how is it related? – shin Jun 30 '16 at 07:20
  • 1
    In the scope of laravel `Tag` is a model. In the scope of OOP `Tag` is an object. In the scope of programming `Tag` is a dedicated space within the language, representing some concept or containing a value. So are variables. – Ivanka Todorova Jun 30 '16 at 07:24