简体   繁体   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?

Ex: 例如:

<?php

class BLAH {

    public static function A () 
    {
        self::B();
        //Do something
    }

    public static function B()
    {
        //Do something
    }

}

Goal: Bind B to A to make sure that B cannot be called elsewhere outside of the static method A. 目标:将B绑定到A,以确保不能在静态方法A之外的其他地方调用B。

I am guessing you want to make B either private or protected , and what you are going for is to make sure B can only be called from inside your class. 我猜您想将B设为privateprotected ,而您要做的是确保B只能从您的班级内部调用。 BUT, if you really want to make sure B can only be called from A : 但是,如果你真的想确保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
     }
}

Simple: Protected method: 简单:受保护的方法:

class BLAH {

    public static function A () 
    {
        self::B();
        //Do something
    }

    protected static function B()
    {
        //Do something
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:我怎样才能得到结果“B,C”或“C,B” - PHP : How can i get the result "B,C" or "C,B" 如何在一个oneliner中随机混淆PHP中的&#39;,&#39;内爆的字符串(例如:a,b,c = c,b,a)? - How do I randomly jumble a string imploded by ',' in PHP (ex: a,b,c = c,b,a) in a oneliner? 如何使用PHP交换字符串中两个不同字符的值? A变成B,B变成A - How can I swap the values of two different characters in a string with PHP? A becomes B, B becomes A 如何使用 php preg_match 从 a{a{} b{}} 转换为 a[a[],b[]]? - How can I convert from a{a{} b{}} to a[a[],b[]] using php preg_match? 在具有2列a和b的2D数组中。 对于a列中所有相同的值,请从php中的b列中选择相应的值? - In a 2 D array with 2 columns a & b. For all the same values in column a, pick corresponding value from column b in php? 我无法作曲家安装 magento/extension-b2b - I can't composer install magento/extension-b2b 在PHP中将字符串编码为a = 1,b = 2,依此类推 - encode string in PHP as a=1,b=2 and so on php我可以在一个外部静态方法中设置一个类属性,该方法由类的非静态方法调用,如果是这样,怎么办? - php can i set a class property in an external static method called by a non-static method of the class, and if so how? 什么是$ a&$ b和$ a | $ B的PHP? - What is $a & $b and $a | $b in php? 我可以将 PHP 运算符 &amp;&amp; 用于 a &amp;&amp; b &amp;&amp; c 之类的 3 个值吗? - Can I use the PHP operator && for 3 values like a && b && c?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM