首页 > 解决方案 > How to make an override for a function in a php file

问题描述

I am using a module which is often updated by the developing company.

In one php file (cat_product_get.php) of this module is the here below function :

function getColSettingsAsXML()

Inside this function is the following code :

        {
            foreach($colSettings[$col]['options'] AS $k => $v)
            {
                $xml.='<option value="'.str_replace('"','\'',$k).'"><![CDATA['.$v.']]></option>';
            }
        }

Such code leads to the following warning : PHP Warning: Invalid argument supplied for foreach() in ../cat_product_get.php on line 317

Line 317 is the following :

foreach($colSettings[$col]['options'] AS $k => $v)

To fix the warning, I added one line as follows :

        if (is_array($colSettings[$col]['options']) || is_object($colSettings[$col]['options']))

        {
            foreach($colSettings[$col]['options'] AS $k => $v)
            {
                $xml.='<option value="'.str_replace('"','\'',$k).'"><![CDATA['.$v.']]></option>';
            }
        }

But at each module update, I have to amend again the cat_product_get.php.

I tried to convince many times the developer to add the suited line in his code. But the developer refused to do it.

Is there a way to add somewhere an override to avoid the line addition at each module update ?

I am not a developer...

I thank you in advance for any reply.

Patrick

标签: php

解决方案


如果该函数只是一个普通函数,那么它不能在 PHP 中完成。您不能有重复的函数名称。

如果它是类中的函数(也称为方法),您可以扩展该类并重写该函数,然后使用您的类。

class Person
{
   public function fixThisCrap()
   {
      return 'the wrong stuff';
   }

   // more functions 
}

class MyPerson extends Person
{
   public function fixThisCrap()
   {
      return 'the correct stuff';
   }

   // parent class functions will be available, no need to code
}

推荐阅读