How to use a custom function file to override a function in another php

Source: Internet
Author: User
How to use a custom function file to rewrite functions in another php file to avoid modifying the system file of the program, so as to prevent files modified after the upgrade from being overwritten during the upgrade, so I want to create a new user-defined function library file: extention. php: include this extention when the system is running. php. you can modify the system functions in php in extention. write your own functions in php.

For example
There are three php files
A. php system library file cannot be modified
Extention. php custom function library file, add and modify at will
Result. php calls the method file in the function library

A. php
Function show ()
{
$ Str = 'This is ';
Return $ str;
}

Extention. php I also wrote a show () function in it. I want to modify the show () method in a. php.
Function show ()
{
$ Str = 'This is my extention ';
Return $ str;
}

Result. php includes the above two php files and calls the show () method.
Include 'A. php ';
Include 'extention. php ';

$ Result = show ();
Echo $ result;

In this way, the Cannot redeclare error will be reported when you access result. php.

I found a document on the Internet and said I could use a namespace, but it seems that the effect is not what I want.
I don't want to touch the system's function library file, such as. php; only modify my extention. php user-defined function library file, so that. modify the functions in php. I don't know if I can do it?


Reply to discussion (solution)

Inherit the system function library and write the same function name

A. php
Function show ()
{
$ Str = 'This is ';
Return $ str;
}

Extention. php I also wrote a show () function in it. I want to modify the show () method in a. php.
Class extention extends {

Function show ()
{
$ Str = 'This is my extention ';
Return $ str;
}
}

Two options:

1. namespace

2. directly modify the target function

Not all of your a. php files cannot be changed.

We recommend that you use the PHP namespace to call functions with the same name.

For 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 call
include('b.php');include('c.php');echo \testb\test();echo \testc\test();

No!
Your idea is that php cannot be implemented because php does not support heavy load.

Although the namespace mentioned above can alleviate this conflict
But it is not suitable for application scenarios, because you do not intend to modify the original system code, and function calls are initiated by the original system.

When designing reloaded system functions, I used the following solutions:
System functions are defined:

   
The "override" function is:
    

However, there is still a difference with your current needs, so we can consider using a program to process a. php. below is the simple test code of the solution:
A. php
   get';}


Extention. php
     

Result. php
    '), '', $a);$a = str_replace(array("\r\n", "\r", "\n"), '', $a);$a = str_replace('function ', PHP_EOL.'function ', $a);$a_fun = explode(PHP_EOL, $a);$new_a = '';foreach($a_fun as $fun) {    if (preg_match('|function\s(.*?)\(|i', $fun, $match)) {        $fun_name = trim($match[1]);        if (!function_exists($fun_name)) {            $new_a .= $fun.PHP_EOL;        }    }}eval($new_a);show();


This can also be optimized:
First, determine whether the. runtime. php file exists. if it does not exist, execute the above process and write $ new_a to a. runtime. php and include it in.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.