class_alias 函数:
// class_alias — 为一个类创建别名
class_alias('original_name', 'alias_name');
class_exists 函数:
// class_exists — 检查类是否已定义
if (class_exists('MyClass')) {
$myclass = new MyClass();
}
get_called_class 函数:
// get_called_class — 后期调用静态方法的类名
class foo {
public static function test() {
var_dump(get_called_class());
}
}
class bar extends foo {
}
foo::test(); // 输出:foo
bar::test(); // 输出:bar
get_class_methods 函数:
// get_class_methods — 返回由类的方法名组成的数组
$class_methods = get_class_methods('myclass');
// 或
$class_methods = get_class_methods(new myclass());
get_class_vars、get_class 函数:
// get_class_vars — 返回由类的默认属性组成的数组
// get_class — 返回对象的类名
$my_class = new myclass();
$class_vars = get_class_vars(get_class($my_class));
get_declared_classes 函数:
// get_declared_classes — 返回由已定义类的名字所组成的数组
$class_arr = get_declared_classes();
print_r($class_arr);
get_declared_interfaces 函数:
// get_declared_interfaces — 返回一个数组包含所有已声明的接口
$interface_arr = get_declared_interfaces();
print_r($interfaces);
get_declared_traits 函数:
// get_declared_traits — 返回所有已定义的 traits 的数组
$trait_arr = get_declared_traits();
print_r($trait_arr);
get_object_vars 函数:
// get_object_vars — 返回由对象属性组成的关联数组
$p1 = new Point2D(1.233, 3.445);
print_r(get_object_vars($p1));
get_parent_class 函数:
// get_parent_class — 返回对象或类的父类名
class foo {
public static function test() {
var_dump(get_called_class());
}
}
class bar extends foo {
function child()
{
echo "I'm " , get_parent_class('bar') , "'s son too\n";
}
}
foo::test(); // 输出:foo
bar::test(); // 输出:I'm dad's son
interface_exists 函数:
// interface_exists — 检查接口是否已被定义
if (interface_exists('MyInterface')) {
class MyClass implements MyInterface
{
// Methods
}
}
is_a 函数:
// is_a — 如果对象属于该类则返回 TRUE
class WidgetFactory
{
var $oink = 'moo';
}
$WF = new WidgetFactory();
if (is_a($WF, 'WidgetFactory')) {
echo "yes, \$WF is still a WidgetFactory\n";
}
is_subclass_of 函数:
// is_subclass_of — 如果此对象是该类的子类,则返回 TRUE
class WidgetFactory
{
}
class WidgetFactory_Child extends WidgetFactory
{
}
$WF = new WidgetFactory();
$WFC = new WidgetFactory_Child();
var_dump(is_subclass_of($WFC, 'WidgetFactory')); // true
var_dump(is_subclass_of($WF, 'WidgetFactory')); // false
method_exists 函数:
// method_exists — 检查类的方法是否存在
$directory = new Directory('.');
var_dump(method_exists($directory,'read'));
var_dump(method_exists('Directory','read'));
property_exists 函数:
// property_exists — 检查对象或类是否具有该属性
class myClass {
public $mine;
private $xpto;
static protected $test;
static function test() {
var_dump(property_exists('myClass', 'xpto')); //true
}
}
var_dump(property_exists('myClass', 'mine')); //true
var_dump(property_exists(new myClass, 'mine')); //true
call_user_func_array 函数:
// call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2\n";
}
}
call_user_func_array("foobar", array("one", "two"));
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
call_user_func 函数:
// call_user_func — 把第一个参数作为回调函数调用
function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func('increment', $a);
func_get_arg、func_get_args、func_num_args 函数:
// func_get_arg — 返回参数列表的某一项
// func_get_args — 返回一个包含函数参数列表的数组
// func_num_args — 返回传递给函数的参数个数
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs<br />\n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "<br />\n";
}
var_dump(func_get_args()); // 打印传入的所有参数
}
foo (1, 2, 3);
function_exists 函数:
// function_exists — 如果给定的函数已经被定义就返回 TRUE
var_dump(function_exists('imap_open'));
register_shutdown_function 函数:
// register_shutdown_function — 注册一个函数在关闭时执行
function shutdown()
{
echo 'Script executed with success', PHP_EOL;
}
register_shutdown_function('shutdown');
本文详细介绍了PHP中与类和对象相关的函数,包括class_alias、class_exists、get_called_class、get_class_methods等,涵盖了类的创建、检查、属性和方法的获取等多个方面,帮助开发者更好地理解和使用PHP的面向对象特性。
183

被折叠的 条评论
为什么被折叠?



