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

Detailed explanation of PHP function libraries and objects

Feb 07, 2020 pm 04:15 PM
php

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
Copy after login

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);
}
Copy after login

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
Copy after login

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';
    }
}
Copy after login

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
Copy after login

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
Copy after login

In actual situations, the operator instanceof

$foo instanceof Foo; // true
$foo instanceof A; // true
$foo instanceof B; // true
Copy after login

is more commonly used to operate

Related functions:

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

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
Copy after login

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);
}
Copy after login

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
Copy after login

Example - Get the exception class when an exception is thrown

throw (new ModelNotFoundException)->setModel(get_called_class());
Copy after login

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",
// ]
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles