php - is isset way is faster than array_search?

Theory speaking, looking into hashmap (isset) is faster than array_search or in_array. But, let’s see how it work in php in practice. Look at this code block: function detectGender($keyword) { $gender = 0; $genderKeywords = [ 'MY' => [ 1 => 'men,male', 2 => 'women,female', ], 'SG' => [ 1 => 'men,male', 2 => 'women,female', ], 'HK' => [ 1 => 'men,male', 2 => 'women,female', ], 'PH' => [ 1 => 'men,male', 2 => 'women,female', ], 'TH' => [ 1 => 'ชาย,ผชาย', 2 => 'หญง,ผหญง', ], 'VN' => [ 1 => 'nam', 2 => 'nu', ], 'ID' => [ 1 => 'pria', 2 => 'wanita', ], ]; foreach ($genderKeywords['VN'] as $key => $keywords) { if (array_search($keyword, explode(',', $keywords)) !...

March 13, 2018 · 3 min · longka

Mockery exception: could not load mock class already exists

Mockery\Exception\RuntimeException: Could not load mock , class already exists When working with those spaghetti legacy code base, sometimes, it is fucking hard to write the test along with refactoring. We may face with some situation that need to mock the public static function call. class Foo { public function bar() { Utils::staticFunction(); // do another thing } } And the test class: use \Mockery as m; use Foo; class FooTest { public function testBar() { $out = new Foo(); $utils = m::mock('alias:Utils'); $util->shouldReceive('staticFunction')->withAnyArgs(); $out->bar(); // do another mock and test } } We use mockery to mock the static function call, run the test with single function, it’s green....

July 5, 2017 · 1 min · longka

pagekit-formmaker extension event handler

pagekit-formmaker is a good extension of pagekit cms for dealing with user feedback https://github.com/Bixie/pagekit-formmaker Beside good functionality, there are quite lots configurable options when playing with this extension. For eg: if you wanna add a Cc email after client submit a feedback Well, let’s take a look at their code, in this line App::trigger('formmaker.form.submission', [$form, $submission]); It triggers an event after saving client’s submission to the database. The event name is formmaker....

May 1, 2017 · 1 min · longka

php ? (?? and ?:) == (!empty and isset)

Simple, but sometimes we use it in wrong way! $a = ''; echo $a ?: 'a string'; $b = ''; echo $b ?? 'b string'; So: isset ~ ?? !empty ~ ?: More example: $a = ['1' => ['2' => []]]; print($a['1']['2'] ?: 'a string'); // print out s string ~ !empty echo PHP_EOL; $b = ['1' => ['2' => []]]; print($b['1']['2'] ?? 'b string'); // print out Array ~ isset ...

February 13, 2017 · 1 min · longka

ISP - interface segregation principle in SOLID

Interface segregation principle (ISP) - tạm dịch: chả biết dịch sao cho sát nghĩa ??, cái từ segregation dịch ra là tách rời ra. Interface thì khỏi phải giải thích rồi nhỉ :) - anw, let’s move on … Nguyên tắc này được diễn giải theo nguyên bản tiếng anh theo Uncle Bob “Client should not be forced to implement an interface that it doesn’t use” - tức là các lớp con (derived classes) không nên hiện thực những phương thức (method) mà nó không cần dùng đến....

August 11, 2016 · 4 min · longka

Contextual binding in laravel and how to make your code meet O in SOLID principles

Sometimes, we meet these kind of use cases or features when doing web application programming: 1 - multiple type of report to generate 2- multiple type of discount to handle 3- multiple type of payment to handle I recently faced with this use case when doing a freelance project for my customer. The requirement for this use case is that we have 2 types of reporting: human resources reporting and accounting reporting....

April 29, 2016 · 4 min · longka

Join with multiple condition in eloquent

Sometimes, you want to use join or left join or right join, etc … in eloquent, here is one example $orders = $this->order ->select(['orders.*']) ->join('customers', 'customers.id', '=', 'orders.customer_id') ->leftJoin('logs', function($join) { $join->on('orders.id', '=', 'logs.entity_id'); $join->where('logs.name', '=', EventsLog::EVENT_EMAIL_SENT_ORDER_PAYMENT_REMINDER); $join->where('logs.entity_type', '=', 'order'); }) ->where('orders.status', Order::STATUS_CREATED) ->where('orders.created_at', '<=', Carbon::now()->subDay($days)) ->whereNull('logs.entity_id') ->get(); return $orders; This will be same as raw sql query (postgres): select orders.id, customers.firstname, customers.lastname, logs.entity_id from orders join customers on (orders....

April 1, 2016 · 1 min · longka

Một vài lưu ý khi cài đặt php7 (php-fpm) từ source code trên linux (fedora)

Giả sử bạn muốn cài đặt trên fedora, php7, php-fpm với nginx là proxy hứng request đẩy qua cho php xử lý (thông qua php-fpm). Khi cài đặt từ source code có thể bạn sẽ gặp những vấn đề sau: Thiếu các thư viện khi chạy lệnh ./congigure Chỉ định đường dẫn đến thư mục sẽ cài php mà bạn muốn( hữu ích khi bạn muốn cài nhiều phiên bản php trên môi trường làm việc của bạn)....

March 1, 2016 · 2 min · longka

Release optivo mail client for laravel

Optivo is quite big mail service provider in Germany https://www.optivo.com/en. This is one of optivo mail client for laravel (use Optivo HTTP API) https://github.com/nguyentienlong/laravel-optivo https://packagist.org/packages/longka/laravel-optivo Todo: write an article about how to create your own laravel package like this package...

February 28, 2016 · 1 min · longka

Vọc vạch View trong laravel

Bài này dịch từ bài này ;) Khi làm việc với laravel, chắc hẳn bạn dùng rất nhiều đoạn code này. Nó rất tiện lợi trong việc trả về một view sau khi controller thực thi các tác vụ liên quan đến business requirement. return view('a/b/c.d.e.f', ['param' => $value]); Tuy nhiên, nó làm việc thế nào, hơi bị ảo đúng không nhỉ? Cùng đào sâu tìm hiểu thử xem thế nào?...

February 2, 2016 · 4 min · longka