= PHP_5_3_0, PHP_7, PHP_8 // class_exists() - PHP_4, PHP_5, PHP_7, PHP_8 // OFFLINE | enum_exists() - PHP_8 >= PHP_8_1_0 // get_called_class() - PHP_5 >= PHP_5_3_0, PHP_7, PHP_8 // get_class_methods() - PHP_4, PHP_5, PHP_7, PHP_8 // get_class_vars() - PHP_4, PHP_5, PHP_7, PHP_8 // get_class() - PHP_4, PHP_5, PHP_7, PHP_8 // get_declared_classes() - PHP_4, PHP_5, PHP_7, PHP_8 // get_declared_interfaces() - PHP_5, PHP_7, PHP_8 // get_declared_traits() - PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // get_mangled_object_vars() - PHP_7 >= PHP_7_4_0, PHP_8 // get_object_vars() - PHP_4, PHP_5, PHP_7, PHP_8 // get_parent_class() - PHP_4, PHP_5, PHP_7, PHP_8 // interface_exists() - PHP_5 >= PHP_5_0_2, PHP_7, PHP_8 // is_a() - PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // is_subclass_of() - PHP_4, PHP_5, PHP_7, PHP_8 // method_exists() - PHP_4, PHP_5, PHP_7, PHP_8 // property_exists() - PHP_5 >= PHP_5_1_0, PHP_7, PHP_8 // trait_exists() - PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // ============================== USING CLASSES (0) // ============================== USING DATA_TYPES (7) // void // string // bool // array // object // false // mixed // ============================== END // REQUIREMENTS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS // ============================== ABOUT // PHP Manual / Function Reference / Variable and Type Related Extensions / Classes/Objects - Class/Object Information // URL: https://www.php.net/manual/en/book.classobj.php // ============================== DESCRIPTION // CLASS_OBJECT_INFORMATION // LANGUAGE_REFERENCE_TYPES_OBJECTS // // CLASS_OBJECT_INFORMATION - BEGIN // Class/Object Information // // INTRODUCTION // INSTALLING_CONFIGURING // PREDEFINED_CONSTANTS // EXAMPLES // CLASSES_OBJECT_FUNCTIONS // // INTRODUCTION - BEGIN // Introduction // // These functions allow you to obtain information about classes and instance objects. You can obtain the name of the class to which an object belongs, as well as its member properties and methods. Using these functions, you can find out not only the class membership of an object, but also its parentage (i.e. what class is the object class extending). // Please see the Objects section of the manual for a detailed explanation of how classes and objects are implemented and used in PHP. // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/intro.classobj.php // INTRODUCTION - END // // INSTALLING_CONFIGURING - BEGIN // Installing/Configuring // // REQUIREMENTS // INSTALLATION // RUNTIME_CONFIGURATION // RESOURCE_TYPES // // REQUIREMENTS - BEGIN // Requirements // // No external libraries are needed to build this extension. // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/classobj.requirements.php // REQUIREMENTS - END // // INSTALLATION - BEGIN // Installation // // There is no installation needed to use these functions; they are part of the PHP core. // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/classobj.installation.php // INSTALLATION - END // // RUNTIME_CONFIGURATION - BEGIN // Runtime Configuration // // This extension has no configuration directives defined in php.ini. // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/classobj.configuration.php // RUNTIME_CONFIGURATION - END // // RESOURCE_TYPES - BEGIN // Resource Types // // This extension has no resource types defined. // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/classobj.resources.php // RESOURCE_TYPES - END // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/classobj.setup.php // INSTALLING_CONFIGURING - END // // PREDEFINED_CONSTANTS - BEGIN // Predefined Constants // // This extension has no constants defined. // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/classobj.constants.php // PREDEFINED_CONSTANTS - END // // EXAMPLES - BEGIN // Examples // // [example] // In this example, we first define a base class and an extension of the class. The base class describes a general vegetable, whether it is edible, and what is its color. The subclass Spinach adds a method to cook it and another to find out if it is cooked. // Example #1 Class Definitions // Vegetable // [php] // // class Vegetable { // public $edible; // // public $color; // // public function __construct($edible, $color = "green") // { // $this->edible = $edible; // $this->color = $color; // } // // public function isEdible() // { // return $this->edible; // } // // public function getColor() // { // return $this->color; // } // } // // [/php] // Spinach // [php] // // class Spinach extends Vegetable { // public $cooked = false; // // public function __construct() // { // parent::__construct(true, "green"); // } // // public function cook() // { // $this->cooked = true; // } // // public function isCooked() // { // return $this->cooked; // } // } // // [/php] // We then instantiate 2 objects from these classes and print out information about them, including their class parentage. We also define some utility functions, mainly to have a nice printout of the variables. // [/example] // [example] // Example #2 test_script.php // [php] // // // register autoloader to load classes // spl_autoload_register(); // // function printProperties($obj) // { // foreach (get_object_vars($obj) as $prop => $val) { // echo "\t$prop = $val\n"; // } // } // // function printMethods($obj) // { // $arr = get_class_methods(get_class($obj)); // foreach ($arr as $method) { // echo "\tfunction $method()\n"; // } // } // // function objectBelongsTo($obj, $class) // { // if (is_subclass_of($obj, $class)) { // echo "Object belongs to class " . get_class($obj); // echo ", a subclass of $class\n"; // } else { // echo "Object does not belong to a subclass of $class\n"; // } // } // // // instantiate 2 objects // $veggie = new Vegetable(true, "blue"); // $leafy = new Spinach(); // // // print out information about objects // echo "veggie: CLASS " . get_class($veggie) . "\n"; // echo "leafy: CLASS " . get_class($leafy); // echo ", PARENT " . get_parent_class($leafy) . "\n"; // // // show veggie properties // echo "\nveggie: Properties\n"; // printProperties($veggie); // // // and leafy methods // echo "\nleafy: Methods\n"; // printMethods($leafy); // // echo "\nParentage:\n"; // objectBelongsTo($leafy, Spinach::class); // objectBelongsTo($leafy, Vegetable::class); // // [/php] // The above examples will output: // [result] // veggie: CLASS Vegetable // leafy: CLASS Spinach, PARENT Vegetable // // veggie: Properties // edible = 1 // color = blue // // leafy: Methods // function __construct() // function cook() // function isCooked() // function isEdible() // function getColor() // // Parentage: // Object does not belong to a subclass of Spinach // Object belongs to class Spinach, a subclass of Vegetable // [/result] // One important thing to note in the example above is that the object $leafy is an instance of the class Spinach which is a subclass of Vegetable. // [/example] // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/classobj.examples.php // EXAMPLES - END // // CLASSES_OBJECT_FUNCTIONS - BEGIN // Classes/Object Functions // // Table of Contents // * __autoload - Attempt to load undefined class // * class_alias - Creates an alias for a class // * class_exists - Checks if the class has been defined // * enum_exists - Checks if the enum has been defined // * get_called_class - The "Late Static Binding" class name // * get_class_methods - Gets the class methods' names // * get_class_vars - Get the default properties of the class // * get_class - Returns the name of the class of an object // * get_declared_classes - Returns an array with the name of the defined classes // * get_declared_interfaces - Returns an array of all declared interfaces // * get_declared_traits - Returns an array of all declared traits // * get_mangled_object_vars - Returns an array of mangled object properties // * get_object_vars - Gets the properties of the given object // * get_parent_class - Retrieves the parent class name for object or class // * interface_exists - Checks if the interface has been defined // * is_a - Checks whether the object is of a given type or subtype // * is_subclass_of - Checks if the object has this class as one of its parents or implements it // * method_exists - Checks if the class method exists // * property_exists - Checks if the object or class has a property // * trait_exists - Checks if the trait exists // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/ref.classobj.php // CLASSES_OBJECT_FUNCTIONS - END // // LITERATURE_SOURCES // * PHP_NET (2023-10-27) // URL: https://www.php.net/manual/en/book.classobj.php // CLASS_OBJECT_INFORMATION - END // // LANGUAGE_REFERENCE_TYPES_OBJECTS - BEGIN // Objects (PHP Manual / Language Reference / Types / Objects) // // Object Initialization // To create a new object, use the new statement to instantiate a class: // [php] // class foo // { // function do_foo() // { // echo "Doing foo."; // } // } // // $bar = new foo; // $bar->do_foo(); // [/php] // For a full discussion, see the Classes and Objects chapter. // // Converting to object // If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was null, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values. Note that in this case before PHP 7.2.0 numeric keys have been inaccessible unless iterated. // [php] // $obj = (object) array('1' => 'foo'); // var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously // var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously // [/php] // For any other value, a member variable named scalar will contain the value. // [php] // $obj = (object) 'ciao'; // echo $obj->scalar; // outputs 'ciao' // [/php] // // LITERATURE_SOURCES // * PHP_NET (2023-11-11) // URL: https://www.php.net/manual/en/language.types.object.php // LANGUAGE_REFERENCE_TYPES_OBJECTS - END // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS___AUTOLOAD // ============================== OFFLINE // ============================== ABOUT // Attempt to load undefined class. // // Warning: This function has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged. // ============================== SUPPORT // PHP_5 - PHP_7 // ============================== USING FUNCTIONS (1) // __autoload() - PHP_5, PHP_7 // ============================== CODE /* function php_variable_classesobjects___autoload($class) { // ========== __AUTOLOAD - BEGIN // ===== ABOUT // Attempt to load undefined class // Warning: This function has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged. // ===== DESCRIPTION // You can define this function to enable classes autoloading. // ===== SUPPORTED // PHP_5, PHP_7 // ===== SYNTAX // __autoload(string $class): void // ===== CODE __autoload( $class // string class - Name of the class to load ); // Return Values // No value is returned. // ===== LITERATURE_SOURCES // * PHP_NET (2023-11-02) // URL: https://www.php.net/manual/en/function.autoload.php // ========== __AUTOLOAD - END // SYNTAX: // void __autoload(string $class) // Return: void } */ // ============================== END // PHP_VARIABLE_CLASSESOBJECTS___AUTOLOAD // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_CLASS_ALIAS // ============================== PUBLIC // ============================== ABOUT // Creates an alias for a class. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // class_alias() - PHP_5 >= PHP_5_3_0, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_class_alias($class, $alias, $autoload = true) { $return_class_alias = false; // ========== CLASS_ALIAS - BEGIN // ===== ABOUT // Creates an alias for a class // ===== DESCRIPTION // Creates an alias named alias based on the user defined class class. The aliased class is exactly the same as the original class. // ===== SUPPORTED // PHP_5 >= PHP_5_3_0, PHP_7, PHP_8 // ===== SYNTAX // class_alias(string $class, string $alias, bool $autoload = true): bool // ===== CODE $return_class_alias = class_alias( $class, // string class - The original class. $alias, // string alias - The alias name for the class. $autoload // bool autoload - Whether to autoload if the original class is not found. ); // Return Values // Returns true on success or false on failure. // // [examples] // Examples // [example] // Example #1 class_alias() example // [php] // // class Foo { } // // class_alias('Foo', 'Bar'); // // $a = new Foo; // $b = new Bar; // // // the objects are the same // var_dump($a == $b, $a === $b); // var_dump($a instanceof $b); // // // the classes are the same // var_dump($a instanceof Foo); // var_dump($a instanceof Bar); // // var_dump($b instanceof Foo); // var_dump($b instanceof Bar); // // [/php] // The above example will output: // [result] // bool(true) // bool(false) // bool(true) // bool(true) // bool(true) // bool(true) // bool(true) // [/result] // [/example] // [/examples] // // Notes // Note: Class names are case-insensitive in PHP, and this is reflected in this function. Aliases created by class_alias() are declared in lowercase. This means that for a class MyClass, the class_alias('MyClass', 'MyClassAlias') call will declare a new class alias named myclassalias. // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.class-alias.php // ========== CLASS_ALIAS - END // SYNTAX: // bool class_alias(string $class, string $alias, bool $autoload = true) return $return_class_alias; // bool } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_CLASS_ALIAS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_CLASS_EXISTS // ============================== PUBLIC // ============================== ABOUT // Checks if the class has been defined. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // class_exists() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_class_exists($class, $autoload = true) { $return_class_exists = false; // ========== CLASS_EXISTS - BEGIN // ===== ABOUT // Checks if the class has been defined // ===== DESCRIPTION // This function checks whether or not the given class has been defined. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // class_exists(string $class, bool $autoload = true): bool // ===== CODE $return_class_exists = class_exists( $class, // string class - The class name. The name is matched in a case-insensitive manner. $autoload // bool autoload - Whether to autoload if not already loaded. ); // Return Values // Returns true if class is a defined class, false otherwise. // // [examples] // Examples // [example] // Example #1 class_exists() example // [php] // // Check that the class exists before trying to use it // if (class_exists('MyClass')) { // $myclass = new MyClass(); // } // // [/php] // [/example] // [example] // Example #2 autoload parameter example // [php] // spl_autoload_register(function ($class_name) { // include $class_name . '.php'; // // // Check to see whether the include declared the class // if (!class_exists($class_name, false)) { // throw new LogicException("Unable to load class: $class_name"); // } // }); // // if (class_exists(MyClass::class)) { // $myclass = new MyClass(); // } // // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.class-exists.php // ========== CLASS_EXISTS - END // SYNTAX: // bool class_exists(string $class, bool $autoload = true) return $return_class_exists; // bool } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_CLASS_EXISTS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_ENUM_EXISTS // ============================== OFFLINE // ============================== ABOUT // Checks if the enum has been defined. // ============================== SUPPORT // PHP_8 // ============================== USING FUNCTIONS (1) // enum_exists() - PHP_8 >= PHP_8_1_0 // ============================== CODE /* function php_variable_classesobjects_enum_exists($enum, $autoload = true) { $return_enum_exists = false; // ========== ENUM_EXISTS - BEGIN // ===== ABOUT // Checks if the enum has been defined // ===== DESCRIPTION // This function checks whether or not the given enum has been defined. // ===== SUPPORTED // PHP_8 >= PHP_8_1_0 // ===== SYNTAX // enum_exists(string $enum, bool $autoload = true): bool // ===== CODE $return_enum_exists = enum_exists( $enum, // string enum - The enum name. The name is matched in a case-insensitive manner. $autoload // bool autoload - Whether to autoload if not already loaded. ); // Return Values // Returns true if enum is a defined enum, false otherwise. // // [examples] // Examples // [example] // Example #1 enum_exists() example // [php] // // Check that the enum exists before trying to use it // if (enum_exists(Suit::class)) { // $myclass = Suit::Hearts; // } // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.enum-exists.php // ========== ENUM_EXISTS - END // SYNTAX: // bool enum_exists(string $enum, bool $autoload = true) return $return_enum_exists; // bool } */ // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_ENUM_EXISTS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_CALLED_CLASS // ============================== PUBLIC // ============================== ABOUT // The "Late Static Binding" class name. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // get_called_class() - PHP_5 >= PHP_5_3_0, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_called_class() { $return_get_called_class = null; // ========== GET_CALLED_CLASS - BEGIN // ===== ABOUT // The "Late Static Binding" class name // ===== DESCRIPTION // Gets the name of the class the static method is called in. // ===== SUPPORTED // PHP_5 >= PHP_5_3_0, PHP_7, PHP_8 // ===== SYNTAX // get_called_class(): string // ===== CODE $return_get_called_class = get_called_class( // This function has no parameters. ); // Return Values // Returns the class name. // // Errors/Exceptions // If get_called_class() is called from outside a class, an Error is thrown. Prior to PHP 8.0.0, an E_WARNING level error was raised. // // Changelog // Version - Description // 8.0.0 - Calling this function from outside a class, will now throw an Error. Previously, an E_WARNING was raised and the function returned false. // // [examples] // Examples // [example] // Example #1 Using get_called_class() // [php] // // class foo { // static public function test() { // var_dump(get_called_class()); // } // } // // class bar extends foo { // } // // foo::test(); // bar::test(); // // [/php] // The above example will output: // [result] // string(3) "foo" // string(3) "bar" // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-called-class.php // ========== GET_CALLED_CLASS - END // SYNTAX: // string get_called_class() return $return_get_called_class; // string } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_CALLED_CLASS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_CLASS_METHODS // ============================== PUBLIC // ============================== ABOUT // Gets the class methods' names. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // get_class_methods() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_class_methods($object_or_class) { $return_get_class_methods = null; // ========== GET_CLASS_METHODS - BEGIN // ===== ABOUT // Gets the class methods' names // ===== DESCRIPTION // Gets the class methods names. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // get_class_methods(object|string $object_or_class): array // ===== CODE $return_get_class_methods = get_class_methods( $object_or_class // object|string object_or_class - The class name or an object instance ); // Return Values // Returns an array of method names defined for the class specified by object_or_class. // // Changelog // Version - Description // 8.0.0 - The object_or_class parameter now only accepts objects or valid class names. // // [examples] // Examples // [example] // Example #1 get_class_methods() example // [php] // // class myclass { // // constructor // function __construct() // { // return(true); // } // // // method 1 // function myfunc1() // { // return(true); // } // // // method 2 // function myfunc2() // { // return(true); // } // } // // $class_methods = get_class_methods('myclass'); // // or // $class_methods = get_class_methods(new myclass()); // // foreach ($class_methods as $method_name) { // echo "$method_name\n"; // } // // [/php] // The above example will output: // [result] // __construct // myfunc1 // myfunc2 // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-class-methods.php // ========== GET_CLASS_METHODS - END // SYNTAX: // array get_class_methods(object|string $object_or_class) return $return_get_class_methods; // array } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_CLASS_METHODS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_CLASS_VARS // ============================== PUBLIC // ============================== ABOUT // Get the default properties of the class. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // get_class_vars() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_class_vars($class) { $return_get_class_vars = null; // ========== GET_CLASS_VARS - BEGIN // ===== ABOUT // Get the default properties of the class // ===== DESCRIPTION // Get the default properties of the given class. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // get_class_vars(string $class): array // ===== CODE $return_get_class_vars = get_class_vars( $class // string class - The class name ); // Return Values // Returns an associative array of declared properties visible from the current scope, with their default value. The resulting array elements are in the form of varname => value. In case of an error, it returns false. // // [examples] // Examples // [example] // Example #1 get_class_vars() example // [php] // // class myclass { // // var $var1; // this has no default value... // var $var2 = "xyz"; // var $var3 = 100; // private $var4; // // // constructor // function __construct() { // // change some properties // $this->var1 = "foo"; // $this->var2 = "bar"; // return true; // } // // } // // $my_class = new myclass(); // // $class_vars = get_class_vars(get_class($my_class)); // // foreach ($class_vars as $name => $value) { // echo "$name : $value\n"; // } // // [/php] // The above example will output: // [result] // var1 : // var2 : xyz // var3 : 100 // [/result] // [/example] // [example] // Example #2 get_class_vars() and scoping behaviour // [php] // function format($array) // { // return implode('|', array_keys($array)) . "\r\n"; // } // // class TestCase // { // public $a = 1; // protected $b = 2; // private $c = 3; // // public static function expose() // { // echo format(get_class_vars(__CLASS__)); // } // } // // TestCase::expose(); // echo format(get_class_vars('TestCase')); // [/php] // The above example will output: // [result] // // 5.0.0 // a| * b| TestCase c // a| * b| TestCase c // // // 5.0.1 - 5.0.2 // a|b|c // a|b|c // // // 5.0.3 + // a|b|c // a // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-class-vars.php // ========== GET_CLASS_VARS - END // SYNTAX: // array get_class_vars(string $class) return $return_get_class_vars; // array } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_CLASS_VARS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_CLASS // ============================== PUBLIC // ============================== ABOUT // Returns the name of the class of an object. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // get_class() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_class($object) { $return_get_class = null; // ========== GET_CLASS - BEGIN // ===== ABOUT // Returns the name of the class of an object // ===== DESCRIPTION // Gets the name of the class of the given object. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // get_class(object $object = ?): string // ===== CODE $return_get_class = get_class( $object // object object - The tested object. This parameter may be omitted when inside a class. // Note: Explicitly passing null as the object is no longer allowed as of PHP 7.2.0 and emits an E_WARNING. As of PHP 8.0.0, a TypeError is emitted when null is used. ); // Return Values // Returns the name of the class of which object is an instance. // If object is omitted when inside a class, the name of that class is returned. // If the object is an instance of a class which exists in a namespace, the qualified namespaced name of that class is returned. // // Errors/Exceptions // If get_class() is called with anything other than an object, TypeError is raised. Prior to PHP 8.0.0, an E_WARNING level error was raised. // If get_class() is called with no arguments from outside a class, an Error is thrown. Prior to PHP 8.0.0, an E_WARNING level error was raised. // // Changelog // Version - Description // 8.0.0 - Calling this function from outside a class, without any arguments, will now throw an Error. Previously, an E_WARNING was raised and the function returned false. // 7.2.0 - Prior to this version the default value for object was null and it had the same effect as not passing any value. Now null has been removed as the default value for object, and is no longer a valid input. // // [examples] // Examples // [example] // Example #1 Using get_class() // [php] // // class foo { // function name() // { // echo "My name is " , get_class($this) , "\n"; // } // } // // // create an object // $bar = new foo(); // // // external call // echo "Its name is " , get_class($bar) , "\n"; // // // internal call // $bar->name(); // // [/php] // The above example will output: // [result] // Its name is foo // My name is foo // [/result] // [/example] // [example] // Example #2 Using get_class() in superclass // [php] // // abstract class bar { // public function __construct() // { // var_dump(get_class($this)); // var_dump(get_class()); // } // } // // class foo extends bar { // } // // new foo; // // [/php] // The above example will output: // [result] // string(3) "foo" // string(3) "bar" // [/result] // [/example] // [example] // Example #3 Using get_class() with namespaced classes // [php] // // namespace Foo\Bar; // // class Baz { // public function __construct() // { // // } // } // // $baz = new \Foo\Bar\Baz; // // var_dump(get_class($baz)); // [/php] // The above example will output: // [result] // string(11) "Foo\Bar\Baz" // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-class.php // ========== GET_CLASS - END // SYNTAX: // string get_class(object $object) return $return_get_class; // string } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_CLASS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_DECLARED_CLASSES // ============================== PUBLIC // ============================== ABOUT // Returns an array with the name of the defined classes. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // get_declared_classes() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_declared_classes() { $return_get_declared_classes = null; // ========== GET_DECLARED_CLASSES - BEGIN // ===== ABOUT // Returns an array with the name of the defined classes // ===== DESCRIPTION // Gets the declared classes. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // get_declared_classes(): array // ===== CODE $return_get_declared_classes = get_declared_classes( // This function has no parameters. ); // Return Values // Returns an array of the names of the declared classes in the current script. // Note: Note that depending on what extensions you have compiled or loaded into PHP, additional classes could be present. This means that you will not be able to define your own classes using these names. There is a list of predefined classes in the Predefined Classes section of the appendices. // // Changelog // Version - Description // 7.4.0 - Previously get_declared_classes() always returned parent classes before child classes. This is no longer the case. No particular order is guaranteed for the get_declared_classes() return value. // // [examples] // Examples // [example] // Example #1 get_declared_classes() example // [php] // print_r(get_declared_classes()); // [/php] // The above example will output something similar to: // [result] // Array // ( // [0] => stdClass // [1] => __PHP_Incomplete_Class // [2] => Directory // ) // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-declared-classes.php // ========== GET_DECLARED_CLASSES - END // SYNTAX: // array get_declared_classes() return $return_get_declared_classes; // array } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_DECLARED_CLASSES // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_DECLARED_INTERFACES // ============================== PUBLIC // ============================== ABOUT // Returns an array of all declared interfaces. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // get_declared_interfaces() - PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_declared_interfaces() { $return_get_declared_interfaces = null; // ========== GET_DECLARED_INTERFACES - BEGIN // ===== ABOUT // Returns an array of all declared interfaces // ===== DESCRIPTION // Gets the declared interfaces. // ===== SUPPORTED // PHP_5, PHP_7, PHP_8 // ===== SYNTAX // get_declared_interfaces(): array // ===== CODE $return_get_declared_interfaces = get_declared_interfaces( // This function has no parameters. ); // Return Values // Returns an array of the names of the declared interfaces in the current script. // // [examples] // Examples // [example] // Example #1 get_declared_interfaces() example // [php] // print_r(get_declared_interfaces()); // [/php] // The above example will output something similar to: // [result] // Array // ( // [0] => Traversable // [1] => IteratorAggregate // [2] => Iterator // [3] => ArrayAccess // [4] => reflector // [5] => RecursiveIterator // [6] => SeekableIterator // ) // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-declared-interfaces.php // ========== GET_DECLARED_INTERFACES - END // SYNTAX: // array get_declared_interfaces() return $return_get_declared_interfaces; // array } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_DECLARED_INTERFACES // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_DECLARED_TRAITS // ============================== PUBLIC // ============================== ABOUT // Returns an array of all declared traits. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // get_declared_traits() - PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_declared_traits() { $return_get_declared_traits = null; // ========== GET_DECLARED_TRAITS - BEGIN // ===== ABOUT // Returns an array of all declared traits // ===== DESCRIPTION // ===== SUPPORTED // PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // ===== SYNTAX // get_declared_traits(): array // ===== CODE $return_get_declared_traits = get_declared_traits( // This function has no parameters. ); // Return Values // Returns an array with names of all declared traits in values. // ===== LITERATURE_SOURCES // * PHP_NET (2023-11-03) // URL: https://www.php.net/manual/en/function.get-declared-traits.php // ========== GET_DECLARED_TRAITS - END // SYNTAX: // array get_declared_traits() return $return_get_declared_traits; // array } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_DECLARED_TRAITS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_MANGLED_OBJECT_VARS // ============================== OFFLINE // ============================== ABOUT // Returns an array of mangled object properties. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // get_mangled_object_vars() - PHP_7 >= PHP_7_4_0, PHP_8 // ============================== CODE /* function php_variable_classesobjects_get_mangled_object_vars($object) { $return_get_mangled_object_vars = null; // ========== GET_MANGLED_OBJECT_VARS - BEGIN // ===== ABOUT // Returns an array of mangled object properties // ===== DESCRIPTION // Returns an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: private variables have the class name prepended to the variable name, and protected variables have a * prepended to the variable name. These prepended values have NUL bytes on either side. Uninitialized typed properties are silently discarded. // ===== SUPPORTED // PHP_7 >= PHP_7_4_0, PHP_8 // ===== SYNTAX // get_mangled_object_vars(object $object): array // ===== CODE $return_get_mangled_object_vars = get_mangled_object_vars( $object // object object - An object instance. ); // Return Values // Returns an array containing all properties, regardless of visibility, of object. // // [examples] // Examples // [example] // Example #1 get_mangled_object_vars() example // [php] // // class A // { // public $public = 1; // // protected $protected = 2; // // private $private = 3; // } // // class B extends A // { // private $private = 4; // } // // $object = new B; // $object->dynamic = 5; // $object->{'6'} = 6; // // var_dump(get_mangled_object_vars($object)); // // class AO extends ArrayObject // { // private $private = 1; // } // // $arrayObject = new AO(['x' => 'y']); // $arrayObject->dynamic = 2; // // var_dump(get_mangled_object_vars($arrayObject)); // [/php] // The above example will output: // [result] // array(6) { // ["Bprivate"]=> // int(4) // ["public"]=> // int(1) // ["*protected"]=> // int(2) // ["Aprivate"]=> // int(3) // ["dynamic"]=> // int(5) // [6]=> // int(6) // } // array(2) { // ["AOprivate"]=> // int(1) // ["dynamic"]=> // int(2) // } // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-mangled-object-vars.php // ========== GET_MANGLED_OBJECT_VARS - END // SYNTAX: // array get_mangled_object_vars(object $object) return $return_get_mangled_object_vars; // array } */ // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_MANGLED_OBJECT_VARS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_OBJECT_VARS // ============================== PUBLIC // ============================== ABOUT // Gets the properties of the given object. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // get_object_vars() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_object_vars($object) { $return_get_object_vars = null; // ========== GET_OBJECT_VARS - BEGIN // ===== ABOUT // Gets the properties of the given object // ===== DESCRIPTION // Gets the accessible non-static properties of the given object according to scope. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // get_object_vars(object $object): array // ===== CODE $return_get_object_vars = get_object_vars( $object // object object - An object instance. ); // Return Values // Returns an associative array of defined object accessible non-static properties for the specified object in scope. // // [examples] // Examples // [example] // Example #1 Use of get_object_vars() // [php] // // class foo { // private $a; // public $b = 1; // public $c; // private $d; // static $e; // // public function test() { // var_dump(get_object_vars($this)); // } // } // // $test = new foo; // var_dump(get_object_vars($test)); // // $test->test(); // // [/php] // The above example will output: // [result] // array(2) { // ["b"]=> // int(1) // ["c"]=> // NULL // } // array(4) { // ["a"]=> // NULL // ["b"]=> // int(1) // ["c"]=> // NULL // ["d"]=> // NULL // } // [/result] // Note: Uninitialized properties are considered inaccessible, and thus will not be included in the array. // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-object-vars.php // ========== GET_OBJECT_VARS - END // SYNTAX: // array get_object_vars(object $object) return $return_get_object_vars; // array } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_OBJECT_VARS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_GET_PARENT_CLASS // ============================== PUBLIC // ============================== ABOUT // Retrieves the parent class name for object or class. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // get_parent_class() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_get_parent_class($object_or_class) { $return_get_parent_class = false; // ========== GET_PARENT_CLASS - BEGIN // ===== ABOUT // Retrieves the parent class name for object or class // ===== DESCRIPTION // Retrieves the parent class name for object or class. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // get_parent_class(object|string $object_or_class = ?): string|false // ===== CODE $return_get_parent_class = get_parent_class( $object_or_class // object|string object_or_class - The tested object or class name. This parameter is optional if called from the object's method. ); // Return Values // Returns the name of the parent class of the class of which object_or_class is an instance or the name. // Note: If the object does not have a parent or the class given does not exist false will be returned. // If called without parameter outside object, this function returns false. // // Changelog // Version - Description // 8.0.0 - The object_or_class parameter now only accepts objects or valid class names. // // [examples] // Examples // [example] // Example #1 Using get_parent_class() // [php] // // class Dad { // function __construct() // { // // implements some logic // } // } // // class Child extends Dad { // function __construct() // { // echo "I'm " , get_parent_class($this) , "'s son\n"; // } // } // // class Child2 extends Dad { // function __construct() // { // echo "I'm " , get_parent_class('child2') , "'s son too\n"; // } // } // // $foo = new child(); // $bar = new child2(); // // [/php] // The above example will output: // [result] // I'm Dad's son // I'm Dad's son too // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.get-parent-class.php // ========== GET_PARENT_CLASS - END // SYNTAX: // string|false get_parent_class(object|string $object_or_class) return $return_get_parent_class; // string|false } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_GET_PARENT_CLASS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_INTERFACE_EXISTS // ============================== PUBLIC // ============================== ABOUT // Checks if the interface has been defined. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // interface_exists() - PHP_5 >= PHP_5_0_2, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_interface_exists($interface, $autoload = true) { $return_interface_exists = false; // ========== INTERFACE_EXISTS - BEGIN // ===== ABOUT // Checks if the interface has been defined // ===== DESCRIPTION // Checks if the given interface has been defined. // ===== SUPPORTED // PHP_5 >= PHP_5_0_2, PHP_7, PHP_8 // ===== SYNTAX // interface_exists(string $interface, bool $autoload = true): bool // ===== CODE $return_interface_exists = interface_exists( $interface, // string interface - The interface name $autoload // bool autoload - Whether to autoload if not already loaded. ); // Return Values // Returns true if the interface given by interface has been defined, false otherwise. // // [examples] // Examples // [example] // Example #1 interface_exists() example // [php] // // Check the interface exists before trying to use it // if (interface_exists('MyInterface')) { // class MyClass implements MyInterface // { // // Methods // } // } // // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.interface-exists.php // ========== INTERFACE_EXISTS - END // SYNTAX: // bool interface_exists(string $interface, bool $autoload = true) return $return_interface_exists; // bool } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_INTERFACE_EXISTS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_IS_A // ============================== PUBLIC // ============================== ABOUT // Checks whether the object is of a given type or subtype. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // is_a() - PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_is_a($object_or_class, $class, $allow_string = false) { $return_is_a = false; // ========== IS_A - BEGIN // ===== ABOUT // Checks whether the object is of a given type or subtype // ===== DESCRIPTION // Checks if the given object_or_class is of this object type or has this object type as one of its supertypes. // ===== SUPPORTED // PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // is_a(mixed $object_or_class, string $class, bool $allow_string = false): bool // ===== CODE $return_is_a = is_a( $object_or_class, // mixed object_or_class - A class name or an object instance. $class, // string class - The class or interface name $allow_string // bool allow_string - If this parameter set to false, string class name as object_or_class is not allowed. This also prevents from calling autoloader if the class doesn't exist. ); // Return Values // Returns true if the object is of this object type or has this object type as one of its supertypes, false otherwise. // // [examples] // Examples // [example] // Example #1 is_a() example // [php] // // define a class // class WidgetFactory // { // var $oink = 'moo'; // } // // // create a new object // $WF = new WidgetFactory(); // // if (is_a($WF, 'WidgetFactory')) { // echo "yes, \$WF is still a WidgetFactory\n"; // } // [/php] // [/example] // [example] // Example #2 Using the instanceof operator // [php] // if ($WF instanceof WidgetFactory) { // echo 'Yes, $WF is a WidgetFactory'; // } // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.is-a.php // ========== IS_A - END // SYNTAX: // bool is_a(mixed $object_or_class, string $class, bool $allow_string = false) return $return_is_a; // bool } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_IS_A // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_IS_SUBCLASS_OF // ============================== PUBLIC // ============================== ABOUT // Checks if the object has this class as one of its parents or implements it. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // is_subclass_of() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_is_subclass_of($object_or_class, $class, $allow_string = true) { $return_is_subclass_of = false; // ========== IS_SUBCLASS_OF - BEGIN // ===== ABOUT // Checks if the object has this class as one of its parents or implements it // ===== DESCRIPTION // Checks if the given object_or_class has the class class as one of its parents or implements it. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // is_subclass_of(mixed $object_or_class, string $class, bool $allow_string = true): bool // ===== CODE $return_is_subclass_of = is_subclass_of( $object_or_class, // mixed object_or_class - A class name or an object instance. No error is generated if the class does not exist. $class, // string class - The class name $allow_string // bool allow_string - If this parameter set to false, string class name as object_or_class is not allowed. This also prevents from calling autoloader if the class doesn't exist. ); // Return Values // This function returns true if the object object_or_class, belongs to a class which is a subclass of class, false otherwise. // // [examples] // Examples // [example] // Example #1 is_subclass_of() example // [php] // // define a class // class WidgetFactory // { // var $oink = 'moo'; // } // // // define a child class // class WidgetFactory_Child extends WidgetFactory // { // var $oink = 'oink'; // } // // // create a new object // $WF = new WidgetFactory(); // $WFC = new WidgetFactory_Child(); // // if (is_subclass_of($WFC, 'WidgetFactory')) { // echo "yes, \$WFC is a subclass of WidgetFactory\n"; // } else { // echo "no, \$WFC is not a subclass of WidgetFactory\n"; // } // // // if (is_subclass_of($WF, 'WidgetFactory')) { // echo "yes, \$WF is a subclass of WidgetFactory\n"; // } else { // echo "no, \$WF is not a subclass of WidgetFactory\n"; // } // // // if (is_subclass_of('WidgetFactory_Child', 'WidgetFactory')) { // echo "yes, WidgetFactory_Child is a subclass of WidgetFactory\n"; // } else { // echo "no, WidgetFactory_Child is not a subclass of WidgetFactory\n"; // } // [/php] // The above example will output: // [result] // yes, $WFC is a subclass of WidgetFactory // no, $WF is not a subclass of WidgetFactory // yes, WidgetFactory_Child is a subclass of WidgetFactory // [/result] // [/example] // [example] // Example #2 is_subclass_of() using interface example // [php] // // Define the Interface // interface MyInterface // { // public function MyFunction(); // } // // // Define the class implementation of the interface // class MyClass implements MyInterface // { // public function MyFunction() // { // return "MyClass Implements MyInterface!"; // } // } // // // Instantiate the object // $my_object = new MyClass; // // // Works since 5.3.7 // // // Test using the object instance of the class // if (is_subclass_of($my_object, 'MyInterface')) { // echo "Yes, \$my_object is a subclass of MyInterface\n"; // } else { // echo "No, \$my_object is not a subclass of MyInterface\n"; // } // // // Test using a string of the class name // if (is_subclass_of('MyClass', 'MyInterface')) { // echo "Yes, MyClass is a subclass of MyInterface\n"; // } else { // echo "No, MyClass is not a subclass of MyInterface\n"; // } // [/php] // The above example will output: // [result] // Yes, $my_object is a subclass of MyInterface // Yes, MyClass is a subclass of MyInterface // [/result] // [/example] // [/examples] // // Notes // Note: Using this function will use any registered autoloaders if the class is not already known. // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.is-subclass-of.php // ========== IS_SUBCLASS_OF - END // SYNTAX: // bool is_subclass_of(mixed $object_or_class, string $class, bool $allow_string = true) return $return_is_subclass_of; // bool } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_IS_SUBCLASS_OF // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_METHOD_EXISTS // ============================== PUBLIC // ============================== ABOUT // Checks if the class method exists. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // method_exists() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_method_exists($object_or_class, $method) { $return_method_exists = false; // ========== METHOD_EXISTS - BEGIN // ===== ABOUT // Checks if the class method exists // ===== DESCRIPTION // Checks if the class method exists in the given object_or_class. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // method_exists(object|string $object_or_class, string $method): bool // ===== CODE $return_method_exists = method_exists( $object_or_class, // object|string object_or_class - An object instance or a class name $method // string method - The method name ); // Return Values // Returns true if the method given by method has been defined for the given object_or_class, false otherwise. // // [examples] // Examples // [example] // Example #1 method_exists() example // [php] // $directory = new Directory('.'); // var_dump(method_exists($directory,'read')); // [/php] // The above example will output: // [result] // bool(true) // [/result] // [/example] // [example] // Example #2 Static method_exists() example // [php] // var_dump(method_exists('Directory','read')); // [/php] // The above example will output: // [result] // bool(true) // [/result] // [/example] // [/examples] // // Notes // Note: Using this function will use any registered autoloaders if the class is not already known. // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.method-exists.php // ========== METHOD_EXISTS - END // SYNTAX: // bool method_exists(object|string $object_or_class, string $method) return $return_method_exists; // bool } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_METHOD_EXISTS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_PROPERTY_EXISTS // ============================== PUBLIC // ============================== ABOUT // Checks if the object or class has a property. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // property_exists() - PHP_5 >= PHP_5_1_0, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_property_exists($object_or_class, $property) { $return_property_exists = false; // ========== PROPERTY_EXISTS - BEGIN // ===== ABOUT // Checks if the object or class has a property // ===== DESCRIPTION // This function checks if the given property exists in the specified class. // Note: As opposed with isset(), property_exists() returns true even if the property has the value null. // ===== SUPPORTED // PHP_5 >= PHP_5_1_0, PHP_7, PHP_8 // ===== SYNTAX // property_exists(object|string $object_or_class, string $property): bool // ===== CODE $return_property_exists = property_exists( $object_or_class, // object|string object_or_class - The class name or an object of the class to test for $property // string property - The name of the property ); // Return Values // Returns true if the property exists, false if it doesn't exist or null in case of an error. // // [examples] // Examples // [example] // Example #1 A property_exists() example // [php] // // class myClass { // public $mine; // private $xpto; // static protected $test; // // static function test() { // var_dump(property_exists('myClass', 'xpto')); //true // } // } // // var_dump(property_exists('myClass', 'mine')); //true // var_dump(property_exists(new myClass, 'mine')); //true // var_dump(property_exists('myClass', 'xpto')); //true // var_dump(property_exists('myClass', 'bar')); //false // var_dump(property_exists('myClass', 'test')); //true // myClass::test(); // // [/php] // [/example] // [/examples] // // Notes // Note: Using this function will use any registered autoloaders if the class is not already known. // Note: The property_exists() function cannot detect properties that are magically accessible using the __get magic method. // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.property-exists.php // ========== PROPERTY_EXISTS - END // SYNTAX: // bool property_exists(object|string $object_or_class, string $property) return $return_property_exists; // bool } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_PROPERTY_EXISTS // ============================== // ============================== BEGIN // PHP_VARIABLE_CLASSESOBJECTS_TRAIT_EXISTS // ============================== PUBLIC // ============================== ABOUT // Checks if the trait exists. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // trait_exists() - PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // ============================== CODE function php_variable_classesobjects_trait_exists($trait, $autoload = true) { $return_trait_exists = false; // ========== TRAIT_EXISTS - BEGIN // ===== ABOUT // Checks if the trait exists // ===== DESCRIPTION // ===== SUPPORTED // PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // ===== SYNTAX // trait_exists(string $trait, bool $autoload = true): bool // ===== CODE $return_trait_exists = trait_exists( $trait, // string trait - Name of the trait to check $autoload // bool autoload - Whether to autoload if not already loaded. ); // Return Values // Returns true if trait exists, and false otherwise. // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-21) // URL: https://www.php.net/manual/en/function.trait-exists.php // ========== TRAIT_EXISTS - END // SYNTAX: // bool trait_exists(string $trait, bool $autoload = true) return $return_trait_exists; // bool } // ============================== END // PHP_VARIABLE_CLASSESOBJECTS_TRAIT_EXISTS // ============================== // ============================== END // PHP_VARIABLE_CLASSESOBJECTS // ============================== ?>