簡體   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