[英]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.