我想阻止foo()被除B以外的任何其他類執行。我如何檢查哪個類創建了對象A?如何獲取創建當前對象的類?
<?php
class A
{
public function foo()
{
if (.... B) // what should be on the dotts?
echo 'I\'m created by class B, which is fine';
else
echo 'Execution of foo() is not allowed';
}
}
class B
{
public function go()
{
$a = new A;
$a->foo();
}
}
class C
{
public function go()
{
$a = new A;
$a->foo();
}
}
$b = new B;
$b->go(); // result: I\'m created by class B, which is fine
$c = New C;
$c->go(); // result: 'Execution of foo() is not allowed'
可能的重複[如何在__construct中調用函數名稱而無需debug_backtrace](http://stackoverflow.com/questions/3308406/how-to-get-called-function-name-in-construct-without-debug -backtrace) – Gordon 2010-08-02 11:16:13