[英]how to change static method in php outside class
We can change the static variable value of a class from outside, thats the advantage of static variables but how do we change static method from outside ? 我们可以从外部更改类的静态变量值,这就是静态变量的优点,但是如何从外部更改静态方法呢?
<?php
class A
{
static $static_var = 0;
static function test(){
return 'i want to change inside this test method';
}
}
echo A::$static_var; // outputs 0
++A::$static_var;
echo A::$static_var; // ouputs 1
// Now how do we do something to change the static test method body? is it possible ?
like
A::test() = function(){ /* this is wrong */}
}
As @Mark Baker said, you can only change variable... But there is a way to declare variable as callable, you can use anonymous function. 正如@Mark Baker所说的,您只能更改变量...但是有一种方法可以将变量声明为可调用的,您可以使用匿名函数。
here is the documentation: http://php.net/manual/en/functions.anonymous.php 这是文档: http : //php.net/manual/zh/functions.anonymous.php
class A
{
public static $method;
}
A::$method = function() {
echo 'A';
};
call_user_func(A::$method);
// OR
$method = A::$method;
$method();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.