PHP在子类方法B调用父类的方法A时,不传参数时仍能得到方法B的名称

在PHP中

class A{

function name(){}

}

class B extends A{

function childName(){ $this->name(); }

}

当子类B调用 childName() 的时候,在不传参数前提下,如何 在父类A 的 name方法中,得到 方法名称 childName ?

class A{
function __call($n,$a){ if( $n=='_call_' ){ $n = $a[0];$this->___name = $n;$this->$n(); } }
function name(){
echo $this->___name; /// childName
}
}

class B extends A{
function childName(){
$this->name();
}
}


用一个index.php做入口文件

<?php
require("B.class.php")
B->_call_( $_GET['a'] );
当然,其中的B可以换成 $_GET['m'],那这就是一个框架的默认入口了。

原文地址:https://www.cnblogs.com/webooxx/p/2360393.html