类与对象
- class_alias - 为一个类创建别名
- class_exists - 检查类是否已定义
- get_called_class - 获取后期静态绑定类的名称
- get_class_methods - 返回由类的方法名组成的数组
- get_class_vars - 返回由类的默认属性组成的数组
- get_class - 返回对象的类名
- get_declared_classes - 返回由已定义类的名字所组成的数组
- get_declared_interfaces - 返回一个数组包含所有已声明的接口
- get_declared_traits - 返回所有已定义的 Trait 的数组
- get_object_vars - 返回由对象属性组成的关联数组
- get_parent_class - 返回对象或类的父类名
- interface_exists - 检查接口是否已被定义
- is_a - 如果对象属于该类或该类是此对象的父类则返回 true
- is_subclass_of - 如果此对象是该类的子类,则返回 true
- method_exists - 检查类的方法是否存在
- property_exists - 检查对象或类是否具有该属性
- trait_exists - 检查指定的 Trait 是否存在
class_alias
<?php
class Foo
{
//
}
var_dump(class_alias('Foo', 'Bar')); // bool(true)
$foo = new Foo();
$bar = new Bar();
var_dump($foo == $bar); // bool(true)
var_dump($foo === $bar); // bool(false)
var_dump($foo instanceof Foo); // bool(true)
var_dump($foo instanceof Bar); // bool(true)
var_dump($bar instanceof Foo); // bool(true)
var_dump($bar instanceof Bar); // bool(true)
class_exists
<?php
class Foo
{
//
}
var_dump(class_exists('Foo')); // bool(true)
var_dump(class_exists('Bar')); // bool(false)
get_called_class
<?php
class Foo
{
/**
* Return the class name of the late static binding.
*
* @param void
* @return string
*/
public static function method(): string
{
return get_called_class();
}
/**
* Return the class name of the late static binding.
*
* @param void
* @return string
*/
public static function call(): string
{
return static::class;
}
}
class Bar extends Foo
{
}
var_dump(Foo::method()); // string(3) "Foo"
var_dump(Bar::method()); // string(3) "Bar"
var_dump(Foo::call()); // string(3) "Foo"
var_dump(Bar::call()); // string(3) "Bar"
get_class_methods
<?php
class Foo
{
/**
* Public method.
*
* @param void
* @return void
*/
public static function public()
{
//
}
/**
* Protected method.
*
* @param void
* @return void
*/
protected static function protected()
{
//
}
/**
* Private method.
*
* @param void
* @return void
*/
private static function private()
{
//
}
/**
* Return all visibility methods.
*
* @param void
* @return array
*/
public static function method(): array
{
return get_class_methods(static::class);
}
}
class Bar extends Foo
{
//
}
$foo = new Foo();
$bar = new Bar();
var_dump(get_class_methods('Foo')); // array(2) { [0]=> string(6) "public" [1]=> string(6) "method" }
var_dump(get_class_methods('Bar')); // array(2) { [0]=> string(6) "public" [1]=> string(6) "method" }
var_dump(get_class_methods($foo)); // array(2) { [0]=> string(6) "public" [1]=> string(6) "method" }
var_dump(get_class_methods($bar)); // array(2) { [0]=> string(6) "public" [1]=> string(6) "method" }
var_dump(Foo::method()); // array(4) { [0]=> string(6) "public" [1]=> string(9) "protected" [2]=> string(7) "private" [3]=> string(6) "method" }
var_dump(Bar::method()); // array(4) { [0]=> string(6) "public" [1]=> string(9) "protected" [2]=> string(7) "private" [3]=> string(6) "method" }
get_class_vars
<?php
class Foo
{
/**
* Public property.
*
* @var string
*/
public string $public = 'public';
/**
* Protected property.
*
* @var string
*/
protected string $protected = 'protected';
/**
* Private property.
*
* @var string
*/
private $private;
/**
* Return all visibility properties.
*
* @param void
* @return array
*/
public static function method(): array
{
return get_class_vars(static::class);
}
}
class Bar extends Foo
{
//
}
var_dump(get_class_vars('Foo')); // array(1) { ["public"]=> string(6) "public" }
var_dump(get_class_vars('Bar')); // array(1) { ["public"]=> string(6) "public" }
var_dump(Foo::method()); // array(3) { ["public"]=> string(6) "public" ["protected"]=> string(9) "protected" ["private"]=> NULL }
var_dump(Bar::method()); // array(3) { ["public"]=> string(6) "public" ["protected"]=> string(9) "protected" ["private"]=> NULL }
get_class
<?php
class Foo
{
/**
* Return the class name of the specified object.
*
* @param void
* @return string
*/
public function method(): string
{
return get_class($this);
}
}
class Bar extends Foo
{
//
}
$foo = new Foo();
$bar = new Bar();
var_dump(get_class($foo)); // string(3) "Foo"
var_dump(get_class($bar)); // string(3) "Bar"
var_dump($foo->method()); // string(3) "Foo"
var_dump($bar->method()); // string(3) "Bar"
get_declared_classes
<?php
var_dump(get_declared_classes());
get_declared_interfaces
<?php
var_dump(get_declared_interfaces());
get_declared_traits
<?php
var_dump(get_declared_traits());
get_object_vars
<?php
class Foo
{
/**
* Public property.
*
* @var string
*/
public string $public = 'public';
/**
* Protected property.
*
* @var string
*/
protected string $protected = 'protected';
/**
* Private property.
*
* @var string
*/
private $private;
/**
* Return all visibility properties.
*
* @param void
* @return array
*/
public function method(): array
{
return get_object_vars($this);
}
}
class Bar extends Foo
{
//
}
$foo = new Foo();
$bar = new Bar();
var_dump(get_object_vars($foo)); // array(1) { ["public"]=> string(6) "public" }
var_dump(get_object_vars($bar)); // array(1) { ["public"]=> string(6) "public" }
var_dump($foo->method()); // array(3) { ["public"]=> string(6) "public" ["protected"]=> string(9) "protected" ["private"]=> NULL }
var_dump($bar->method()); // array(3) { ["public"]=> string(6) "public" ["protected"]=> string(9) "protected" ["private"]=> NULL }
get_parent_class
<?php
class Foo
{
/**
* Return the parent class name of the specified class.
*
* @param void
* @return string|bool
*/
public static function method()
{
return get_parent_class(static::class);
}
}
class Bar extends Foo
{
//
}
$foo = new Foo();
$bar = new Bar();
var_dump(get_parent_class('Foo')); // bool(false)
var_dump(get_parent_class('Bar')); // string(3) "Foo"
var_dump(get_parent_class($foo)); // bool(false)
var_dump(get_parent_class($bar)); // string(3) "Foo"
var_dump(Foo::method()); // bool(false)
var_dump(Bar::method()); // string(3) "Foo"
interface_exists
<?php
interface Foo
{
//
}
var_dump(interface_exists('Foo')); // bool(true)
var_dump(interface_exists('Bar')); // bool(false)
is_a
<?php
class Foo
{
//
}
class Bar extends Foo
{
//
}
$foo = new Foo();
$bar = new Bar();
var_dump(is_a('Foo', 'Foo', false)); // bool(false)
var_dump(is_a('Foo', 'Bar', false)); // bool(false)
var_dump(is_a('Bar', 'Foo', false)); // bool(false)
var_dump(is_a('Bar', 'Bar', false)); // bool(false)
var_dump(is_a('Foo', 'Foo', true)); // bool(true)
var_dump(is_a('Foo', 'Bar', true)); // bool(false)
var_dump(is_a('Bar', 'Foo', true)); // bool(true)
var_dump(is_a('Bar', 'Bar', true)); // bool(true)
var_dump(is_a($foo, 'Foo')); // bool(true)
var_dump(is_a($foo, 'Bar')); // bool(false)
var_dump(is_a($bar, 'Foo')); // bool(true)
var_dump(is_a($bar, 'Bar')); // bool(true)
is_subclass_of
<?php
class Foo
{
//
}
class Bar extends Foo
{
//
}
$foo = new Foo();
$bar = new Bar();
var_dump(is_subclass_of('Foo', 'Foo', false)); // bool(false)
var_dump(is_subclass_of('Foo', 'Bar', false)); // bool(false)
var_dump(is_subclass_of('Bar', 'Foo', false)); // bool(false)
var_dump(is_subclass_of('Bar', 'Bar', false)); // bool(false)
var_dump(is_subclass_of('Foo', 'Foo', true)); // bool(false)
var_dump(is_subclass_of('Foo', 'Bar', true)); // bool(false)
var_dump(is_subclass_of('Bar', 'Foo', true)); // bool(true)
var_dump(is_subclass_of('Bar', 'Bar', true)); // bool(false)
var_dump(is_subclass_of($foo, 'Foo')); // bool(false)
var_dump(is_subclass_of($foo, 'Bar')); // bool(false)
var_dump(is_subclass_of($bar, 'Foo')); // bool(true)
var_dump(is_subclass_of($bar, 'Bar')); // bool(false)
method_exists
<?php
class Foo
{
/**
* Public method.
*
* @param void
* @return void
*/
public static function public()
{
//
}
/**
* Protected method.
*
* @param void
* @return void
*/
protected static function protected()
{
//
}
/**
* Private method.
*
* @param void
* @return void
*/
private static function private()
{
//
}
}
class Bar extends Foo
{
//
}
$foo = new Foo();
$bar = new Bar();
var_dump(method_exists('Foo', 'public')); // bool(true)
var_dump(method_exists('Foo', 'protected')); // bool(true)
var_dump(method_exists('Foo', 'private')); // bool(true)
var_dump(method_exists('Bar', 'public')); // bool(true)
var_dump(method_exists('Bar', 'protected')); // bool(true)
var_dump(method_exists('Bar', 'private')); // bool(false)
var_dump(method_exists($foo, 'public')); // bool(true)
var_dump(method_exists($foo, 'protected')); // bool(true)
var_dump(method_exists($foo, 'private')); // bool(true)
var_dump(method_exists($bar, 'public')); // bool(true)
var_dump(method_exists($bar, 'protected')); // bool(true)
var_dump(method_exists($bar, 'private')); // bool(true)
property_exists
<?php
class Foo
{
/**
* Public property.
*
* @var string
*/
public string $public = 'public';
/**
* Protected property.
*
* @var string
*/
protected string $protected = 'protected';
/**
* Private property.
*
* @var string
*/
private $private;
}
class Bar extends Foo
{
//
}
$foo = new Foo();
$bar = new Bar();
var_dump(property_exists('Foo', 'public')); // bool(true)
var_dump(property_exists('Foo', 'protected')); // bool(true)
var_dump(property_exists('Foo', 'private')); // bool(true)
var_dump(property_exists('Bar', 'public')); // bool(true)
var_dump(property_exists('Bar', 'protected')); // bool(true)
var_dump(property_exists('Bar', 'private')); // bool(false)
var_dump(property_exists($foo, 'public')); // bool(true)
var_dump(property_exists($foo, 'protected')); // bool(true)
var_dump(property_exists($foo, 'private')); // bool(true)
var_dump(property_exists($bar, 'public')); // bool(true)
var_dump(property_exists($bar, 'protected')); // bool(true)
var_dump(property_exists($bar, 'private')); // bool(false)
trait_exists
<?php
trait Foo
{
//
}
var_dump(trait_exists('Foo')); // bool(true)
var_dump(trait_exists('Bar')); // bool(false)