A. php中的對象分為哪兩種
得到一個對象的類型,使用gettype()函數:
<?php
echo gettype(1); // 輸出integer
echo gettype(array()); // 輸出array
得到一個對象是哪個類的實例,使用get_class()函數:
<?php
$o = new stdClass();
echo get_class(); // 輸出stdClass
得到一個類或對象的方法和屬性,要使用反射:
<?php
class MyClass {
public $var;
public function foo() {}
}
$ref = new ReflectionClass('MyClass');
$ref->getProperties(); // 會返回一組對象,用法參考PHP手冊
$ref->getMethods(); // 會返回一組對象,用法參考PHP手冊
$obj = new MyClass();
$ref = new ReflectionObject($obj);
$ref->getProperties();
$ref->getMethods();
B. php中怎麼使用ReflectionClass的getMethods方法
你的這個問題完全可以到後盾人那裡解決,那裡有很多相關的視頻教學,我也在那裡學習
C. php怎麼返回類的所有常量屬性
PHP獲取類中常量,屬性,及方法列表的方法
$r=newReflectionClass($this);
Zend_Debug::mp($r->getConstants(),"Constants");
Zend_Debug::mp($r->getProperties(),"Properties");
Zend_Debug::mp($r->getMethods(),"Methods");
D. php 中如何得到一個對象的類型
得到一個對象的類型,使用gettype()函數:
<?php
echogettype(1);//輸出integer
echogettype(array());//輸出array
得到一個對象是哪個類的實例,使用get_class()函數:
<?php
$o=newstdClass();
echoget_class();//輸出stdClass
得到一個類或對象的方法和屬性,要使用反射:
<?php
classMyClass{
public$var;
publicfunctionfoo(){}
}
$ref=newReflectionClass('MyClass');
$ref->getProperties();//會返回一組對象,用法參考PHP手冊
$ref->getMethods();//會返回一組對象,用法參考PHP手冊
$obj=newMyClass();
$ref=newReflectionObject($obj);
$ref->getProperties();
$ref->getMethods();
E. php中怎麼用ReflectionClass中的方法獲取類信息
$class = new ReflectionClass('ClassName');
//獲取ClassName類的屬性
$class->getProperties()
//獲取ClassName類的方法
$class->getMethods()
F. php有沒有什麼函數可以獲取一個方法中的參數名和參數類型的
/**
*獲取一個函數的依賴
*@paramstring|callable$func
*@paramarray$param調用方法時所需參數形參名就是key值
*@returnarray返回方法調用所需依賴
*/
functiongetFucntionParameter($func,$param=[]){
if(!is_array($param)){
$param=[$param];
}
$ReflectionFunc=newReflectionFunction($func);
$depend=array();
foreach($ReflectionFunc->getParameters()as$value){
if(isset($param[$value->name])){
$depend[]=$param[$value->name];
}elseif($value->isDefaultValueAvailable()){
$depend[]=$value->getDefaultValue();
}else{
$tmp=$value->getClass();
if(is_null($tmp)){
thrownewException("{$class}");
}
$depend[]=$this->get($tmp->getName());
}
}
return$depend;
}
functiontest($a,$b=20){
echo$a,',',$b;
}
$depend=getFucntionParameter('test',['a'=>30,'b'=>40]);
call_user_func_array('test',$depend);//30,40
上面的函數是我開發的框架的容器的方法。
php提供了很完整的反射機制。不但可以反射函數,還可以反射方法,反射類構造函數。
G. php中怎麼使用ReflectionClass的getMethods方法
$class = new ReflectionClass('ClassName'); //獲取ClassName類的屬性 $class->getPropert