简体   繁体   中英

Hook a function with uneditable method in PHP

I want to know if this is possible.

Imagine that I am using a PHP library Class which I can not modify. However I want to hook to its method somehow, that whenever it's called, my custom code executes.

For example:

<?php

function uneditable() {
    // does something
}

// my code:
if(<uneditable is called>){
    // do this
}

Is it possible in PHP somehow?

Note: I'm trying to do this with WordPress, as I have to detect execution of some method in a third party plugin which doesn't provide hooks to the endpoint that i'm trying to hook into. I don't want to edit the plugin itself as it will get updated anytime and my code will be gone.

Thank and Regards.

As it is wordpress you can use the Wordpress hook .

Here is an example.

<?php

 /**
  * Define the action and give functionality to the action.
  */
 function uneditable() {
   do_action( 'uneditable' );
 }

 /**
  * Register the action with WordPress.
  */
 add_action( 'uneditable', 'uneditable_hook' );
 function uneditable_hook() {
   echo 'This is a custom action hook.';
 }

You can read more about add_action and do_action

What you need is abstract method in abstract class. That will force the child class to implement the method.

abstract class A {


abstract function a() {}

}

class B extends A {

function a () {


// write your code here.
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM