[英]PHP: I have 2 static functions A and B. How do I bind B to A so that B can only be called within the method A?
例如:
<?php
class BLAH {
public static function A ()
{
self::B();
//Do something
}
public static function B()
{
//Do something
}
}
目标:将B绑定到A,以确保不能在静态方法A之外的其他地方调用B。
我猜您想将B
设为private
或protected
,而您要做的是确保B
只能从您的班级内部调用。 但是,如果你真的想确保B
只能从所谓的A
:
public static function B()
{
$trace=debug_backtrace();
$caller=array_shift($trace);
if ($caller['function'] != 'A' || $caller['class'] != 'BLAH') {
throw new Exception('Illegal function invocation');
}
else {
//do something
}
}
简单:受保护的方法:
class BLAH {
public static function A ()
{
self::B();
//Do something
}
protected static function B()
{
//Do something
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.