Skip to content

Zeroplex 生活隨筆

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

標籤: PHP

透過 composer 的 autoload 設定做 bootstrap

Posted on 2023 年 1 月 12 日2023 年 1 月 12 日 By 日落 在〈透過 composer 的 autoload 設定做 bootstrap〉中尚無留言

使用 symfony/polyfill-intl-idn 後發現有趣的事,不用特別去檢查 PHP extension 的 function 是否存在,只要 require symfony polyfill 以後,完全不需要進行任何設定就可以正常運作。

後來在 composer.json 看到 autoload 的寫法:

{
    "autoload": {
        "psr-4": { "Symfony\\Polyfill\\Intl\\Idn\\": "" },
        "files": [ "bootstrap.php" ]
    },
}

以前以為 autoload 只能挑其中一種方法使用,沒想到可以指定多種方法同時使用。

上面的設定方式,namespace 會在 class 使用時才 require 進來,而 bootstrap.php 則是在一開始就 require 並執行,polyfill 就在當下偵測並處理掉 extension functions。所以使用者 composer require 以後程式並不需要做任何修改。

Share this…
  • Facebook
  • Twitter
  • Telegram
  • Line
  • WordPress
Tags:PHP

Set protected property on PHPUnit mock object

Posted on 2023 年 1 月 8 日2023 年 1 月 8 日 By 日落 在〈Set protected property on PHPUnit mock object〉中尚無留言

假設有個 class:

class Woker
{
    protected Queue $queue;

    public function run()
    {
        while (!$this->queue->isEmpty()) {
            $job = $this->queue->pop();
            // do something
        }
    }
}

用 PHPUnit 建立 stub 以後,必須使用 reflection 來自訂 $queue property:

$worker = $this->getMockBuilder(Worker::class)
    ->disableOriginalConstructor()
    ->getMock();

$queue = $this->createMock(Queue::class);
$queue->expected($this->atLeast(1))
    ->method('isEmpty')
    ->willReturn(true);

// PHP Reflection on $worker
$refProperty = new \ReflectionProperty($worker, 'queue');
$refProperty->setAccessible(true);
$refProperty->setValue($crawler, $queue);

這樣寫 test case 蠻髒的,有點想要換 test framework ….

Share this…
  • Facebook
  • Twitter
  • Telegram
  • Line
  • WordPress
Tags:PHP, PHPUnit

PHP 各版本的重大改變

Posted on 2022 年 12 月 4 日2022 年 12 月 16 日 By 日落 在〈PHP 各版本的重大改變〉中有 2 則留言

這份整理拖太久了,到 PHP 8.2 都上線了還沒寫完,所以就整理到 7.x 結束吧。

PHP 從約 5.2 版就開始慢慢的加上一些重大的改變,像是 PHP v5.3 開始支援 `namespace` 之後,開始有了 composer 各種套件系統的發展。這些改變讓 PHP 的開發和維護更方便、更簡單。以下記錄各版本重要的功能,以便在更新程式時可以更快的了解有什麼需要留意的事項。

備註:我沒用過 PHP 4.x,如果你還在用的話,請多保重 ….

More “PHP 各版本的重大改變” »

Share this…
  • Facebook
  • Twitter
  • Telegram
  • Line
  • WordPress
Tags:PHP

讓 phpMyAdmin 的資料表清單不分頁

Posted on 2022 年 6 月 23 日2022 年 6 月 23 日 By 日落 在〈讓 phpMyAdmin 的資料表清單不分頁〉中尚無留言

新版本的 phpMyAdmin 預設會將資料表過多的清單分頁,以便加快頁面載入速度,但其資料表 filter / searching 只會針對當下的分頁進行搜尋,塑資料表在第二頁或後面的分頁,則會搜尋不到資料。

若希望 phpMyAdmin 在資料表清單中不要分頁,可以在 config.inc.php 設定檔最後新增參數:

$cfg['MaxTableList'] = 1000;
Share this…
  • Facebook
  • Twitter
  • Telegram
  • Line
  • WordPress
Tags:PHP

Laravel Migration 出現 foreign key constraint in complete 注意事項

Posted on 2022 年 4 月 16 日2022 年 4 月 16 日 By 日落 在〈Laravel Migration 出現 foreign key constraint in complete 注意事項〉中尚無留言

今天遇到錯誤訊息耽誤了很久:

... foreign key constraint 'xxx_id_foreign' are incompatible.

錯誤訊息沒有詳細列出可能的錯誤,追了很久才知道有一些地方要注意。

More “Laravel Migration 出現 foreign key constraint in complete 注意事項” »

Share this…
  • Facebook
  • Twitter
  • Telegram
  • Line
  • WordPress
Tags:Database, Laravel, PHP

文章導覽

1 2 ... 23 下一頁

其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

3C Apache AWS Bash C/C++ docker FreeBSD Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 小提琴 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.

Go to mobile version