Created
February 5, 2021 07:22
-
-
Save grandmanitou/ac4683e698946761cedbde27ef81ca1d to your computer and use it in GitHub Desktop.
PHP Proxy function in class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Source: | |
// https://stackoverflow.com/questions/3716649/how-to-auto-call-function-in-php-for-every-other-function-call | |
class test { | |
function __construct(){} | |
private function test1(){ | |
echo "In test1", PHP_EOL; | |
} | |
private function test2(){ | |
echo "test2", PHP_EOL; | |
} | |
protected function test3(){ | |
return "test3" . PHP_EOL; | |
} | |
public function __call($method, $arguments) { | |
if(method_exists($this, $method)) { | |
$this->test1(); | |
return call_user_func_array([$this, $method], $arguments); | |
} | |
} | |
} | |
$a = new test; | |
$a->test2(); | |
echo $a->test3(); | |
/* | |
* Output: | |
* In test1 | |
* test2 | |
* In test1 | |
* test3 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment