php - 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 中完成。您不能有重复的函数名称。
如果它是类中的函数(也称为方法),您可以扩展该类并重写该函数,然后使用您的类。
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
}
推荐阅读
- node.js - 如何使用量角器 webdriver 知道下拉列表的长度?
- java - 如何将字符串行读入双列并将它们保存在方法中
- mysql - Liquibase 部分提交变更集
- ssh - 在 CLion 部署中填充零字节的部署文件
- bash - Concourse CI/CD - 将 concourse 凭据作为 shell 脚本参数传递
- c# - 我应该如何在 asp.net mvc 5 中使用 autofac 注入带有 TokenProvider 的 Autorest 客户端?
- laravel-5 - 如何在刀片 Laravel 中显示代码中的数字
- css - 如何从反应组件的道具设置现有css类的颜色
- jpa - java.lang.IllegalStateException:无法加载缓存项
- sql - 添加计算列时,“as”附近的语法不正确