Home  >  Article  >  Backend Development  >  PHP variables and type extensions such as classes and objects

PHP variables and type extensions such as classes and objects

伊谢尔伦
伊谢尔伦Original
2016-11-21 17:01:491040browse

1. Overview and Installation

These functions allow you to obtain information about classes and object instances. You can get the class name to which the object belongs, as well as its member properties and methods. By using these functions, you can not only find the relationship between objects and classes, but also their inheritance relationship (for example, which class an object class inherits from).

Please refer to the object-oriented chapters to see a detailed description of how objects and classes are implemented and used in PHP.

No installation is required to use these functions, they are part of the PHP core.

2. Complete collection of class and object functions

__autoload — Try to load an undefined class

call_user_method_array — Call a user method while passing parameter array (deprecated)

call_user_method — Call a user method on a specific object (deprecated)

class_alias — Create an alias for a class

class_exists — Check whether the class has been defined

get_called_class — The name of the class for late static binding ("Late Static Binding")

get_class_methods — Return 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_class — Returns the class name of the object

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

get_declared_interfaces — Returns an array containing all declared interfaces

get_declared_traits — Returns an array of all defined traits

get_object_vars — Returns an associative array composed of object attributes

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

interface_exists — Checks whether the interface has been defined

is_a — If Returns TRUE if the object belongs to this class or this class is the parent class of this object

is_subclass_of — If this object is a subclass of this class, returns TRUE

method_exists — Checks whether the method of the class exists

property_exists — Checks the object or Whether the class has this attribute

trait_exists - Check whether the specified trait exists

3. Usage example

In this example, we first define a base class and an extension of the class. This base class describes a common vegetable, regarding whether it is edible and its color. Subclass Spinach adds a method for cooking and another method for checking whether it is cooked.

Example #1 classes.inc

<?php
    // base class with member properties and methods
    class Vegetable {
        var $edible;
        var $color;
        function Vegetable($edible, $color="green") 
        {
            $this->edible = $edible;
            $this->color = $color;
        }
        function is_edible() 
        {
            return $this->edible;
        }
        function what_color() 
        {
             return $this->color;
        }
   } // end of class Vegetable
   // extends the base class
   class Spinach extends Vegetable {
       var $cooked = false;
       function Spinach() 
       {
           $this->Vegetable(true, "green");
       }
       function cook_it() 
       {
           $this->cooked = true;
       }
       function is_cooked() 
       {
           return $this->cooked;
       }
   } // end of class Spinach
?>

Next we instantiate two objects from these classes and print their information, including the inheritance relationship of their classes. At the same time, we also defined some utility functions, mainly to print out these variables beautifully.

Example #2 test_script.php

<?php
    include "classes.inc";
    // 实用函数
    function print_vars($obj)
    {
        foreach (get_object_vars($obj) as $prop => $val) {
            echo "\t$prop = $val\n";
        }
    }
    function print_methods($obj)
    {
        $arr = get_class_methods(get_class($obj));
        foreach ($arr as $method) {
            echo "\tfunction $method()\n";
        }
    }
    function class_parentage($obj, $class)
    {
        if (is_subclass_of($GLOBALS[$obj], $class)) {
            echo "Object $obj belongs to class " . get_class($$obj);
            echo " a subclass of $class\n";
        } else {
            echo "Object $obj does not belong to a subclass of $class\n";
        }
    }
    // 实例化 2 对象
    $veggie = new Vegetable(true, "blue");
    $leafy = new Spinach();
    // 打印这些对象的信息
    echo "veggie: CLASS " . get_class($veggie) . "\n";
    echo "leafy: CLASS " . get_class($leafy);
    echo ", PARENT " . get_parent_class($leafy) . "\n";
    // 显示蔬菜的属性
    echo "\nveggie: Properties\n";
    print_vars($veggie);
    // and leafy methods
    echo "\nleafy: Methods\n";
    print_methods($leafy);
    echo "\nParentage:\n";
    class_parentage("leafy", "Spinach");
    class_parentage("leafy", "Vegetable");
?>

An important thing to note is that in the above example, the object $leafy is an instance of Spinach (a subclass of Vegetable), and the last part of the script will output the following information:

[...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable


Statement:
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
Previous article:PHP observer patternNext article:PHP observer pattern