php 反射 getClass,ReflectionParameter::getClass()

ReflectionParameter::getClass()

(PHP 5, PHP 7)

获得类型提示类。

说明publicReflectionParameter::getClass(void) :ReflectionClass

获取参数的类型提示类,类型为ReflectionClass对象。Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

一个ReflectionClass对象。

范例

使用ReflectionParameter类<?php

function foo(Exception $a) { }

$functionReflection = new ReflectionFunction('foo');

$parameters = $functionReflection->getParameters();

$aParameter = $parameters[0];

echo $aParameter->getClass() >name;

?>

以上例程会输出:Exception

参见ReflectionParameter::getDeclaringClass() Gets declaring classThe method returns ReflectionClass object of parameter type class or NULL if none.

class A {

function b(B $c, array $d, $e) {

}

}

class B {

}

$refl = new ReflectionClass('A');

$par = $refl->getMethod('b')->getParameters();

var_dump($par[0]->getClass() >getName()); // outputs B

var_dump($par[1]->getClass()); // note that array type outputs NULL

var_dump($par[2]->getClass()); // outputs NULL

?>ReflectionParameter::getClass() will cause a fatal error (and trigger __autoload) if the class required by the parameter is not defined.

Sometimes it's useful to only know the class name without needing the class to be loaded.

Here's a simple function that will retrieve only the class name without requiring the class to exist:

function getClassName(ReflectionParameter $param) {

preg_match('/\[\s\\s([\w]+)/s', $param->__toString(), $matches);

return isset($matches[1]) ? $matches[1] : null;

}

?>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值