Home >Backend Development >PHP Tutorial >Detailed explanation of PHP function libraries and objects

Detailed explanation of PHP function libraries and objects

藏色散人
藏色散人forward
2020-02-07 16:15:592583browse

Detailed explanation of PHP function libraries and objects

Deprecated

Some functions have been deprecated or removed, please do not use them

__autoload - Deprecated in version 7.2

call_user_method_array - Removed in version 7.0

call_user_method - Removed in version 7.0

Judgment

Existence check of class

Related functions

class_exists - determine whether the class exists

interface_exists - determine whether the interface exists

trait_exists - determine Whether Trait exists

The second parameter is used to determine whether to use automatic loading if it has not been loaded yet.

class_exists ( string $class_name [, bool $autoload = true ] ) : bool
interface_exists ( string $interface_name [, bool $autoload = true ] ) : bool
trait_exists ( string $traitname [, bool $autoload = true ] ) : bool

Example - Extensive class existence check function

function common_class_exists(string $class): bool
{
    return class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);
}

Existence check of class members

Related functions:

property_exists - Check whether the property exists

method_exists - Check whether the method exists

method_exists ( mixed $object , string $method_name ) : bool
property_exists ( mixed $class , string $property ) : bool

Example-Implement a callback function, the user can define the callback URL through the method or attribute

trait RedirectsUsers
{
    public function redirectPath()
    {
        if (method_exists($this, 'redirectTo')) {
            return $this->redirectTo();
        }
        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
    }
}

Class relationship judgment

Related functions:

is_a — The object belongs to this class or the parent class of this class, returns TRUE

is_subclass_of — The object is the A subclass of the class, returns TRUE

is_a ( object $object , string $class_name [, bool $allow_string = FALSE ] ) : bool
is_subclass_of ( object $object , string $class_name ) : bool

Example

interface A {}
interface B {}
class BaseFoo implements B {}
class Foo extends BaseFoo implements A{}
$foo = new Foo();
// 对象
is_a($foo, 'BaseFoo'); // true
is_a($foo, 'Foo'); // true
is_a($foo, 'A'); // true
// 类
is_a('Foo', 'BaseFoo'); // false
is_a('Foo', 'BaseFoo', true);  // true, 传入第三个参数,代表允许使用类名而不是示例
is_subclass_of($foo, 'Foo'); // false
is_subclass_of($foo, 'BaseFoo'); // true
is_subclass_of($foo, 'B'); // true

In actual situations, the operator instanceof

$foo instanceof Foo; // true
$foo instanceof A; // true
$foo instanceof B; // true

is more commonly used to operate

Related functions:

class_alias() - 为一个类创建别名
class_alias ( string $original , string $alias [, bool $autoload = TRUE ] ) : bool

Example - Category name loader, used to manage aliases of classes

class AliasLoader
{
    private $aliases;
    public function __construct(array $aliases)
    {
        $this->aliases = $aliases;
    }
    public function load($alias)
    {
        if (isset($this->aliases[$alias]))
        {
            return class_alias($this->aliases[$alias], $alias);
        }
    }
}
class LongLongLongLongFoo {}
$aliases = [
    'Foo' => 'LongLongLongLongFoo',
    'Bar' => 'LongLongLongLongBar',
];
$loader =  new AliasLoader($aliases);
$loader->load('Foo');
$foo = new Foo();
var_dump($foo);  // object(LongLongLongLongFoo)#3395

Get

Get all

Related functions:

get_declared_traits — Returns an array of all defined traits

get_declared_interfaces — Returns an array containing all declared interfaces

get_declared_classes — Returns an array consisting of the names of defined classes

These functions are rarely needed in practice

foreach (get_declared_classes() as $class) {
    $r = new \ReflectionClass($class);
}

Get classes

Related functions

get_called_class — The name of the late static binding class, returns false when used outside the class

get_class — Returns the class name of the object

get_parent_class — Returns the object Or the parent class name of the class

get_called_class ( void ) : array
get_class ([ object $object = NULL ] ) : string
get_parent_class ([ mixed $obj ] ) : string

Example - Get the exception class when an exception is thrown

throw (new ModelNotFoundException)->setModel(get_called_class());

Get the class members

Related functions:

get_class_methods — Returns an array consisting of the method names of the class

get_class_vars — Returns an array consisting of the default attributes of the class

get_object_vars — Returns an associative array consisting of object attributes

Example - Get all accessor properties in a class

class Foo {
    public function getFullNameAttribute()
    {
    }
    public function getTextAttribute()
    {
    }
    public static function getMutatorMethods()
    {
        preg_match_all(&#39;/(?<=^|;)get([^;]+?)Attribute(;|$)/&#39;, implode(&#39;;&#39;, get_class_methods(static::class)), $matches);
        return $matches[1];
    }
}
Foo::getMutatorMethods()
// [
//     "FullName",
//     "Text",
// ]

The above is the detailed content of Detailed explanation of PHP function libraries and objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete