PHP function library details and objects

Discard

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

  • __autoload - 7.2 obsolete
  • Call > User > method > array - version 7.0 remove
  • Call "user" method - version 7.0 remove

judge

 

Class existence check

correlation function

  • class_exists - judge whether a class exists
  • Interface? Exists - determines whether an interface exists
  • trait_exists - determines whether a trait exists

The second parameter determines whether to use autoload if it is not already loaded.

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 checking functions

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

Correlation function:

  • property_exists - check if 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 method or property

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

        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
    }
}

  

Class relation judgment

Correlation function:

  • Is a - object belongs to this class or its parent class, return TRUE
  • Is? Subclass? Object is a subclass of this class, return 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();

// object
is_a($foo, 'BaseFoo'); // true
is_a($foo, 'Foo'); // true
is_a($foo, 'A'); // true

// class
is_a('Foo', 'BaseFoo'); // false
is_a('Foo', 'BaseFoo', true);  // true, passing in the third parameter, which means the class name is allowed instead of the example

is_subclass_of($foo, 'Foo'); // false
is_subclass_of($foo, 'BaseFoo'); // true
is_subclass_of($foo, 'B'); // true

  

In practice, the operator instanceof is used more often

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

  

operation

Correlation function:

class_alias() - create an alias for a class

class_alias ( string $original , string $alias [, bool $autoload = TRUE ] ) : bool

  

Example - class alias loader for managing 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

  

Obtain

 

Get all

Correlation function:

  • Get ABCD declared ABCD traits - returns an array of all defined traits
  • Get? Declared? Interfaces - returns an array containing all declared interfaces
  • Get? Declared? Classes - returns an array of the names of defined classes

These functions are rarely used in practice

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

  

Get class

correlation function

  • Get ﹣ called ﹣ class - the name of the late static binding class, which returns false when used outside the class
  • get_class - returns the class name of the object
  • Get > parent > class - returns the name of the parent class of an object or class
get_called_class ( void ) : array
get_class ([ object $object = NULL ] ) : string
get_parent_class ([ mixed $obj ] ) : string

  

Example - the class that gets the exception when it is thrown

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

  

Get class members

Correlation function:

  • Get class methods - returns an array of the class's method names
  • Get class vars - returns an array of default properties of a class
  • Get object vars - returns an associative array of object properties

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

  

Tags: PHP Attribute

Posted on Wed, 06 May 2020 05:42:39 -0400 by webren