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

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

When developing, we tend to create something like a utility class that compiles our own convenient functions without looking at the documentation, but frameworks usually have a certain number of convenient functions.
There are various issues, such as the documentation being in English or there being too many functions to cover them all, so I would like to summarize some of the useful classes and functions in FuelPHP that I found useful.

Arr::pluck()

I'll start from the beginning, but this is very useful.
The English word is not that familiar to me (this is in terms of my vocabulary), so at first glance I didn't understand what it meant, but if you think of it as an extended version of the array_keys function, you'll get the idea.

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 may be a difficult example to understand, but once you use it, you'll understand.
Because the type is checked internally and the processing is separated, it can be used without any problems with pure arrays, but even in the case of arrays, it is not passed to array_keys, so if you know it is an array, it will be faster to simply use array_keys.

 

Event class

In FuelPHP, you can insert processing before or after each process.
Specifically, create event.php under the config directory, and specify it as an array like other configs.

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 will add processing to the array using an anonymous function.
Also, if you want to write processing before the return but don't want to write it globally, there is 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 may prove useful when looping through the number of days in a month.
Ultimately, it's the same as the following, but it's a useful function for people like me who don't want to remember

$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 can be difficult to use, but it increments the suffix number in a string.
When you fill "test1" and "test2" in Excel, "test3" and "test4" are automatically created, just like that.

You can specify the character to separate, or if you specify an empty string, it can handle unseparated strings.
For strings that do not have a number as a suffix, it will automatically add a suffix, but I'm not sure if this is a good idea.

$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 line breaks are also counted as one character.
FuelPHP's Str class is designed with multibyte characters in mind, so it can be used with confidence.

Although FuelPHP is said to be lightweight, it has some useful functions like these.
I would like to release more information once I have accumulated some ideas.

 
That's all

If you found this article useful, please click [Like]!
0
Loading...
0 votes, average: 0.00 / 10
2,765
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 I'm also grateful to be able to do a variety of other work, including marketing.
My portrait rights within Beyond are CC0.