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('/(?<=^|;)get([^;]+?)Attribute(;|$)/', implode(';', 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
