- 首先定义一个类
<?php
Interface trainning
{
public function exec();
}
class mma implements trainning
{
public function exec()
{
return "我是徐晓冬~练MMA找我就对了,北京著名的疯子教练就是我!";
}
}
class muay_thai implements trainning
{
public function exec()
{
return "雅木萨,练泰拳找我就对了,走吧普及岛海滩走一道";
}
}
class sd implements trainning
{
public function exec()
{
return "卧操~还找他妈的隋炀帝吗?要不到樊荣那里去看看";
}
}
class mycoach
{
protected $name;
protected $age;
protected $expertise=array();
public function __construct($name,$age,trainning $martial)
{
$this->name=$name;
$this->age=$age;
$this->type = $martial;
}
public function setskill(Array $skill)
{
$this->skill = $skill;
var_dump($this->skill);
return (Array) $this->skill;
}
public function curriculum()
{
echo $this->type->exec();
}
}
$coach = new ReflectionClass('mycoach');
if ($coach->isInstantiable())
{
echo "可以实例化".PHP_EOL;
}
$myinit = $coach->getConstructor();
- 通过反射获取类的构造函数的入参(或者说依赖)
$dependencies=$myinit->getParameters();
var_dump($dependencies);
foreach($dependencies as $dp)
{
if (is_null($dp->getclass()))
{
echo "只是个普通参数".PHP_EOL;
}
else{
echo "类的名字".$dp->getClass()->name.PHP_EOL;
}
}
输出结果:
array(3) {
[0]=>
object(ReflectionParameter)#3 (1) {
["name"]=>
string(4) "name"
}
[1]=>
object(ReflectionParameter)#4 (1) {
["name"]=>
string(3) "age"
}
[2]=>
object(ReflectionParameter)#5 (1) {
["name"]=>
string(7) "martial"
}
}
array(3) {
[0]=>
object(ReflectionParameter)#3 (1) {
["name"]=>
string(4) "name"
}
[1]=>
object(ReflectionParameter)#4 (1) {
["name"]=>
string(3) "age"
}
[2]=>
object(ReflectionParameter)#5 (1) {
["name"]=>
string(7) "martial"
}
}
只是个普通参数
只是个普通参数
类的名字trainning
- getName getFileName
var_dump("获取列的名字: ".$coach->getName());
var_dump("获取类文件的路径: ".$myinit->getFileName());
输出结果:
string(29) "获取列的名字: mycoach"
string(70) "获取类文件的路径: /home/cpc/PhpstormProjects/untitled/duwa.php"