[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

I can't help but want to use it! I will introduce useful functions of FuelPHP that only I know.

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

When developing something, you tend to create something like a Utility class that is a collection of useful functions that you have created yourself without looking at the documentation, but frameworks usually have a certain amount of useful functions.
There are various things, such as the documentation being in English and the fact that there are too many functions to cover, so I would like to summarize some useful classes and functions of FuelPHP that I thought were useful.

Arr::pluck()

I'll just skip from the beginning, which is very useful.
I didn't know what it was when I looked at it, since I'm not familiar with English words (this is relative to my vocabulary), but I guess an extended version of the array_keys function would make sense.

As the name suggests, array_keys is a function that extracts keys from an array and returns a new array with the keys as values, but Arr::pluck() also works on properties of objects, such as pair models. It's a useful item that I think will come in handy.

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.
Since the processing is separated by internal type checking, it can be used for pure arrays without any problems, but even in the case of an array, it is not passed to array_keys, so if you know that it is an array, you can use it without any problem. It would be faster to use array_keys for

 

Event class

FuelPHP allows you to insert processes before or after each process starts.
Specifically, we will create event.php under the config directory, but like other configs, we will specify it as 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 will add processing to the array using an anonymous function.
Also, if you want to write a process before return, but don't want to write it globally, there are the following methods.

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 for any pattern of access, so be careful not to get too elaborate or you may get strange errors.

 

Date::days_in_month()

As the name suggests, this function returns the number of days in the specified month.

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


 It may be useful when performing loop processing for the number of days included in a certain month.
In the end, it is synonymous with the following, but it is 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; // The result is // ' 30'

 
However, the return value in this case is strictly a string, so there are some differences.

 

Str::increment()

This is also a difficult function to use, but it increments the number corresponding to the suffix in a string.
When you fill in "test1" and "test2" in Excel, "test3" and "test4" are automatically created, which is exactly how it works.

You can also specify characters to separate, and if you specify an empty character, you can also use non-separated strings.
It even automatically adds a suffix to strings that don't have a number as 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($test2); echo Str::increment($test3); echo Str::increment($test4, 1, ''); 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 that PHP code that suddenly adds a number to a string.

 

Str::truncate()

The previous one was increment and now the one is truncate, so is it SQL? That's what it feels like, but it's completely different.

Str::truncate() works by truncating the string after the specified number of characters if the given string has more than the specified number of characters, and returns it with "..." appended by default.

I don't know how many strings will fit in the frame, but I think there is a certain demand for not wanting to break lines or increasing the width.

In such a case, use this function to quickly shorten it.

$test = "It's raining\nIt's raining\nIt's windy making\nIt's snowing, it's summer, it's hot\nIt's strong and strong\nI love it\nIt's hard to decide\nIt's a rice cake that needs to be eaten\nFour cups of brown rice in a day\n nMiso and a little vegetable wotabe\nArayurukotowo\nJibunwo Kanjouni Rezuni\nYokumiki Kishiwakari\nSoshitewasurezu"; // Aozora Bunko (http://www.aozora.gr.jp/cards/000081/ files/45630_23908.html) echo Str::truncate($test, 10); // The result is // 'Ame Nemo Makezu // Kaze Nemo...'


 It seems that the line feed code is also counted as one character.
FuelPHP's Str class is designed with multi-byte characters in mind, so you can use it with peace of mind.

Even though FuelPHP is said to be lightweight, it does have some useful functions like this.
Also, I would like to release it when I have enough material.

 
That's it.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
2,285
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

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 fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.