繁体   English   中英

PHP:我有2个静态函数A和B。如何将B绑定到A,以便只能在方法A中调用B?

[英]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设为privateprotected ,而您要做的是确保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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM