Laravel's Fluent Class - No, not the query-builder.
Today's topic is Laravel's Fluent class. We're not talking about the Fluent query-builder. Fluent is defined as: _Able to express oneself easily and articulately: "a fluent speaker and writer on technical subjects"._ It should be of no surprise that the Fluent class is designed to make it easier to access data from an object or an array. Step 1) Use the Fluent class to 'wrap' an object or array. $my_thoughts = array('cats' => 'sneezing', 'dogs' => 'pooing'); $pets = new \Laravel\Fluent($my_thoughts); Now we can access the data using the new Fluent object. echo $pets->cats; echo $pets->dogs; One benefit is that should you try: echo $my_thoughts['snakes']; You'll get an 'undefined index' error. With the Fluent class you will simply get null. You also have the power of the array_get() helper that we learned yesterday! As you know the array_get() helper allows you to use array dot notation to easily and cleanly retrieve data from arrays. For example, you can do the following: $data = array('people' => array('chrono' => 'ordinary', 'marle' => 'princess', 'lucca' => 'smart', 'frog' => 'honorable')); $fluent = new \Laravel\Fluent($data); // returns 'ordinary' echo $fluent->get('people.chrono'); // returns NULL echo $fluent->get('people.magus'); // returning a default value: returns "nobody knows, man" echo $fluent->get('people.schala', 'nobody knows, man'); In addition you can set values in interesting ways: $fluent = new \Laravel\Fluent(); $fluent->name('Locke'); // returns 'Locke' echo $fluent->name; It's interesting how much functionality you can get from such a small class. This is also a great opportunity to learn more about PHP's magic methods. Marco Monteiro wrote a great guide on this topic. The link to this post can be found below under extracurricular activities. **Our Goal For the Day** Create an object using Laravel's Fluent class then use and understand each function defined within the class (it's a very short class). Think about how you can use this class in your applications. **Required Reading** [Laravel's Fluent class](https://github.com/laravel/laravel/blob/master/laravel/fluent.php) **Extracurricular Activity** Learn and understand PHP's magic methods. [Official PHP Documentation on Magic Methods](http://php.net/manual/en/language.oop5.magic.php) [Marco Monteiro's Blog "PHP Magic Methods - The Complete Guide"](http://blog.marcomonteiro.net/post/33150811883/php-magic-methods)








