【问题标题】:Strategy to overwrite a function in codeigniter 4 system在codeigniter 4系统中覆盖函数的策略
【发布时间】:2022-01-21 14:32:02
【问题描述】:

在 CodeIgniter 4 中有一个类 vendor>codeigniter4>framework>system>HTTP>Files>UploadedFile.php 和一个函数 move() 我想覆盖它。 我想将 return 语句从 true 更改为 $destination

我不想覆盖文件本身,因为这对更新不安全。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    对于您要查找的内容,您需要扩展类并覆盖 move 函数。

    https://codeigniter.com/user_guide/extending/core_classes.html#extending-core-classes

    <?php
    namespace CodeIgniter\HTTP\Files;
    
    class myUploadFile extends UploadedFile implements UploadedFileInterface
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function move(string $targetPath, ?string $name = null, bool $overwrite = false)
        {
            //your code here
        }
    }
    

    【讨论】:

    • 这给了我'CodeIgniter\HTTP\Files\myUploadFile' does not implement methods 'hasMoved', 'getError', 'getName', 'getTempName', 'getClientExtension', 'getClientMimeType', 'isValid', 'getDestination'
    • 我想我需要扩展UploadedFile,是吗?而且我想我也需要将我的班级命名为UploadedFile,对吗?我试过了,但它不起作用。它使用的是供应商类而不是我的。
    • 是的,我很抱歉。那是我的一个错字。您需要扩展 uploadfile 类并引入这些方法。或者你可以删除 implements 接口
    • 但是class UploadedFile extends UploadedFile 位于app\Libraries,它不需要我的课。
    猜你喜欢
    • 2011-05-11
    • 2011-06-19
    • 2022-01-19
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多