简体   繁体   English

PHP,区分内部和外部类方法调用

[英]PHP, distinguish between internal and external class method call

Can't get my head around this, is there any way to check if a method was called internally? 我无法解决这个问题,是否有任何方法可以检查内部是否调用了方法? By this I mean a traceback to check if it was called by $this and not a pointer to the instance. 我的意思是回溯,以检查它是否被$ this调用,而不是实例的指针。 Kind of like the concept of private function but only function is public? 有点像私有功能的概念,但只有功能是公共的?

<?php

class Foo {
    public function check () {
        /*
        if invoked by $this (internally)
            return true
        else
            return false
        */
    }

    public function callCheck () {
        /* returns true because its called by $this */
        return $this->check();
    }
}

$bar = new Foo;
// this should return false because we are calling it from an instance
$bar->check();
// where as this will return true
$bar->callCheck();

?>

Maybe this is undo-able but I really need it for my project at university? 也许这是可以撤消的,但是我在大学的项目中确实需要它吗? Anyone come across a solution or knows how I would identify a solution. 任何人遇到解决方案或知道我将如何确定解决方案。

Thanks. 谢谢。

Below solution does not work. 以下解决方案不起作用。


You could use debug_backtrace but it will be slow. 您可以使用debug_backtrace,但是会很慢。 I really advise you find a different way to solve the problem you are trying to overcome. 我真的建议您找到另一种方法来解决您要克服的问题。

<?php
public function check() {
    $trace = debug_backtrace();
    if ($trace[1]['class'] == 'MyClassName') {
        return true;
    }
    return false;
}

if you there is a call $bar->callCheck(); 如果有电话,则$ bar-> callCheck(); control exits from function check(); 控制从函数check()退出;

first it go to callCheck() then after it goest to check() and return from there 首先去callCheck()然后再去check()然后从那里返回

debug_backtrace(); debug_backtrace(); should work. 应该管用。 place the debug_backtrace(); 放置debug_backtrace(); inside check() method. 内部check()方法。

do this: 做这个:

$t = debug_backtrace(); $ t = debug_backtrace(); var_dump($t); var_dump($ t);

from here you should check $t['function'] and $t['class'], combine those 2 , you should find out is a call, external or internal. 从这里,您应该检查$ t ['function']和$ t ['class'],将这2结合起来,您应该发现是外部调用还是内部调用。

here is out put from my machine, php version is 5.2.14. 这是从我的机器上放出来的,php版本是5.2.14。

array(1) {
  [0]=>
  array(7) {
    ["file"]=>
    string(15) "C:\php\test.php"
    ["line"]=>
    int(24)
    ["function"]=>
    string(5) "check"
    ["class"]=>
    string(3) "Foo"
    ["object"]=>
    object(Foo)#1 (0) {
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}
array(2) {
  [0]=>
  array(7) {
    ["file"]=>
    string(15) "C:\php\test.php"
    ["line"]=>
    int(18)
    ["function"]=>
    string(5) "check"
    ["class"]=>
    string(3) "Foo"
    ["object"]=>
    object(Foo)#1 (0) {
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(7) {
    ["file"]=>
    string(15) "C:\php\test.php"
    ["line"]=>
    int(26)
    ["function"]=>
    string(9) "callCheck"
    ["class"]=>
    string(3) "Foo"
    ["object"]=>
    object(Foo)#1 (0) {
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}

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

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