I'll introduce some useful FuelPHP functions that I know only I've used before!

Hello.
I'm Mandai, the Wild team member in charge of development.

When developing, it's easy to create utility classes that group your own convenient functions without consulting the documentation, but frameworks usually come with a good set of useful functions. Since
documentation can be in English, or the sheer number of features might make it difficult to cover everything, I'd like to compile a list of useful FuelPHP classes and functions that I found particularly helpful.

Arr::pluck()

I'll jump right in, but this is incredibly useful.
The English words are so unfamiliar to me (that's relative to my vocabulary) that at first glance I didn't understand what it meant, but perhaps it will make more sense if I say it's an extended version of the array_keys function.

As the name suggests, array_keys is a function that extracts the keys of an array and returns a new array with the keys as values, but Arr::pluck() is a useful function that also works on object properties and may be useful for paired models

class Item { public $name; public $value; public $color_id; public function __construct($name, $value, $color_id) { $this->name = $name; $this->value = $value; $this->color_id = $color_id; } } $items = []; $items[] = new Item('Scented Eraser (Red)', 150, 1); $items[] = new Item('Scented Eraser (Blue)', 150, 2); $items[] = new Item('Scented Eraser (Yellow)', 150, 3); $color_ids = Arr::pluck($items, 'color_id'); var_export($color_ids); // Result is // array ( // 0 => 1, // 1 => 2, // 2 => 3, // )

 
This might be a rather difficult example to understand, but once you use it, you should understand it.
Because it performs type checking internally and separates the processing, it can be used without problems even with pure arrays, but even with arrays, it doesn't pass the data to `array_keys`, so if you know it's an array, it's probably faster to just use `array_keys`.

 

Event class

FuelPHP allows you to insert code before each process starts or after it finishes.
Specifically, you create an event.php file in the config directory, and like other configuration files, you specify the events using an array.

return [ 'fuelphp' => [ 'app_created' => function(){}, 'request_created' => function(){}, 'request_started' => function(){}, 'controller_started' => function(){}, 'controller_finished' => function(){}, 'response_created' => function(){}, 'request_finished' => function(){}, 'shutdown' => function(){}, ], ];

 
This involves adding processing within the array using an anonymous function.
Also, for those who want to write processing before the return statement but don't want to write it globally, there's the following method:

return call_user_func(funtion(){ // Write common processing for all events here $a = 'hogehoge'; // For example... return [ 'shutdown' => function() use ($a){ Log::debug($a); }, ]; });

 
Events are executed regardless of the access pattern, so be careful not to get too fancy with them as you may get strange errors

 

Date::days_in_month()

As the name suggests, this function returns the number of days in a given month

$year = 2016; $month = 6; $days = Date::days_in_month($month, $year); echo $days; // result is // 30

 
This might prove useful when you need to loop through the number of days in a given month.
the order of the `mktime` arguments,who doesn't want to rememberor doesn't know how to remember them, it's a very helpful function.

$year = 2016; $month = 6; $days = date('j', mktime(0, 0, 0, $month + 1, 0, $year)); echo $days; // results in // '30'

 
However, in this case the return value is technically a string, so there is a slight difference

 

Str::increment()

This is another function that's difficult to find a good use for, but it increments the number corresponding to the suffix in a string. It's
similar to how, when you fill in "test1" and "test2" in Excel, "test3" and "test4" are automatically created.

You can specify the character to use as a separator, and if you specify an empty string, it can handle strings that are not separated.
It will even automatically add a suffix to strings that do not have a number as a suffix, but whether this is a useful feature is debatable.

$test1 = 'test'; $test2 = 'test_1'; $test3 = 'test_3'; $test4 = 'test4'; $test5 = 'test_1_5'; $test6 = 'test_'; echo Str::increment($test1); ''); echo Str::increment($test5); echo Str::increment($test6); // The result is // 'test_1' // 'test_2' // 'test_4' // 'test5' // 'test_1_6' // 'test__1'

 
As an aside, if you look at the source code inside, you'll see some typical PHP code that just adds a number to a string

 

Str::truncate()

The previous command was increment and this command is truncate, so you might think it's SQL, but it's not

Str::truncate() will truncate the string after the specified number of characters if the given string is longer than the specified number of characters, and by default will return "..."

I think there is a fair amount of demand for text that does not require line breaks or widening, even though you do not know how much text will fit within the frame

In such cases, you can quickly shorten it using this function

$test = "Not afraid of rain,\n not afraid of wind,\n not afraid of snow or summer heat,\n I have a strong body,\n I have no desires,\n I never get angry,\n I'm always smiling quietly,\n I eat four cups of brown rice,\n miso paste, and a few vegetables a day,\n I listen carefully, understand, and never forget what's going on,\n without putting my mind to it."; // Excerpt from Aozora Bunko (http://www.aozora.gr.jp/cards/000081/files/45630_23908.html) echo Str::truncate($test, 10); // The result is // 'Not afraid of rain, // not afraid of wind...'

 
It seems that newline characters are also counted as one character.
FuelPHP's Str class is designed with multibyte characters in mind, so you can use it with relative confidence.

Even though FuelPHP is considered lightweight, it still has these kinds of handy little functions.
I'll share more ideas when I have enough.

 
That's all

If you found this article helpful,please give it a "Like"!
0
Loading...
0 votes, average: 0.00 / 10
2,824
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but thankfully I'm also given the opportunity to work on various other tasks, including marketing.
My image rights within Beyond are treated as CC0.