How to rewrite a function in another PHP with a custom function file
This post was last edited by qq37431300 on 2014-01-16 10:28:54
In order not to modify the program's system files, to prevent later upgrade after the modified file will be overwritten by the upgrade, so you want to create a new custom function library file: extention.php, when the system is running Include this extention.php in, so you can modify the system function inside the extention.php can also write their own functions in the extention.php.
Like what
There are 3 PHP files
a.php system function library file, can not modify it
Extention.php your own custom function library files, add and modify them casually
Result.php call the method file inside the function library
a.php
Function Show ()
{
$str = ' This is a ';
return $str;
}
Extention.php I also write a function in the show (), want to modify the a.php inside the show () method
Function Show ()
{
$str = ' This is my extention a ';
return $str;
}
result.php here include the above 2 PHP files and call the show () method
Include ' a.php ';
Include ' extention.php ';
$result = Show ();
echo $result;
This way the access result.php will report cannot redeclare error.
On the Internet to find the information can use the namespace, but it seems that the effect is not what I want.
I would like to not move the system library files, such as the above a.php, only modify my extention.php Custom function library files, so that the functions in the a.php can be modified. I wonder if I can do it?
Share to: more
------Solution--------------------
Inherit the system's function library and write the same function name.
------Solution--------------------
a.php
Function Show ()
{
$str = ' This is a ';
return $str;
}
Extention.php I also write a function in the show (), want to modify the a.php inside the show () method
Class Extention extends a{
Function Show ()
{
$str = ' This is my extention a ';
return $str;
}
}
------Solution--------------------
Two options:
1. Namespaces
2, directly modify the target function
------Solution--------------------
Not all of your a.php should be changed.
It is recommended that you call the PHP namespace to invoke the function with the same name.
Let me give you an example.
test/b.php
namespace Testb;
function Test () {
Return ' b.php ';
}
test/c.php
namespace TESTC;
function Test () {
Return ' c.php ';
}
Test/a.php to invoke
Include (' b.php ');
Include (' c.php ');
Echo \testb\test ();
Echo \testc\test ();
------Solution--------------------
No!
Your idea is not possible in PHP, because PHP does not support overloading
The namespaces mentioned above may alleviate this contradiction.
But not suitable for the application scenario, because you do not intend to modify the original system code, and the function of the call is the original system initiated