PHP variables and type extensions such as objects

Source: Internet
Author: User

I. Overview and Installation

These functions allow you to obtain information about classes and object instances. You can get the class name that the object belongs to, or it can be a member property and method. By using these functions, you can not only find the relationships between objects and classes, but also their inheritance (for example, which class the object class inherits from).

Refer to the Object-oriented section to see a detailed description of how objects and classes are implemented and used in PHP.

Using these functions does not require installation, they are part of the PHP core.

Second, class and object function Daquan

__autoload-attempting to load an undefined class

call_user_method_array-invokes a user method, passing a parameter array (deprecated)

Call_user_method-invoking a user method on a specific object (deprecated)

class_alias-Creating an alias for a class

class_exists-checking whether a class is defined

get_called_class-Late static binding ("late static binding") class name

get_class_methods-returns an array of the method names of the class

get_class_vars-returns an array that consists of the default properties 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 the declared interfaces

get_declared_traits-returns an array of all defined traits

get_object_vars-returns an associative array consisting of object properties

get_parent_class-returns the parent class name of an object or class

interface_exists-Check if the interface has been defined

is_a-returns TRUE if the object belongs to the class or if the class is the parent class of this object

is_subclass_of-returns TRUE If this object is a subclass of the class

method_exists-checking the existence of a class's methods

property_exists-checking whether an object or class has this property

trait_exists-checks if the specified trait exists

Iii. Examples of Use

In this example, we first define a base class and an extension of that class. This base class describes a common vegetable, about whether it is edible and its color. Subclass Spinach Adds a method of cooking and another way to check if it has been 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 of their class. At the same time, we also set some practical functions, mainly to print out these variables beautifully.

Example #2 test_script.php

<?php include "Classes.inc"; Utility 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";    }}//Instantiate 2 object $veggie = new Vegetable (true, "blue");    $leafy = new spinach (); Print the information for these objects echo "Veggie:class". Get_class ($veggie).    "\ n"; echo "Leafy:class".    Get_class ($leafy); echo ", PARENT". Get_parent_class ($leafy).    "\ n";    Displays the properties of the vegetable 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");? >

One important thing to note in the example above is that the object $leafy is an instance of spinach (vegetable subclass), and the last part of the script outputs the following information:

[...] Parentage:object leafy does not belong to a subclass of Spinachobject leafy belongs to class spinach a subclass of Vegetab Le
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.