Skip to content

Instantly share code, notes, and snippets.

@grandmanitou
Created February 5, 2021 07:22
Show Gist options
  • Save grandmanitou/ac4683e698946761cedbde27ef81ca1d to your computer and use it in GitHub Desktop.
Save grandmanitou/ac4683e698946761cedbde27ef81ca1d to your computer and use it in GitHub Desktop.
PHP Proxy function in class
<?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