
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Creating anonymous objects in PHP
Beginning from PHP version 7, it has been made possible to create anonymous classes. Every object in PHP is associated with a class. The anonymous classes can be instantiated to create objects.
Example
<?php class my_sample_class {} $obj = new class extends my_sample_class {}; echo "Does the instance belong to parent class? = " ; echo var_dump($obj instanceof my_sample_class); ?>
Output
Does the instance belong to parent class? = bool(true)
In the above code, a parent class (my_sample_class) has been created, and it has been instantiated with a child class (new class) that inherits from the parent class.
We are checking if the instance belongs to the parent class. Since the child class is an extension of the parent class, it returns True as output.
- Related Articles
- PHP Anonymous classes
- PHP Anonymous functions
- Anonymous classes in PHP 7?
- Creating Instance Objects in Python
- How to implement lambda expression without creating an anonymous class in Java?
- Access variables from parent scope in anonymous PHP function
- PHP Objects.
- Creating and Using Objects using Perl
- PHP Comparing Objects
- Storing objects in PHP session
- Creating an array of objects based on another array of objects JavaScript
- Creating multiple Java objects by one type only
- PHP Objects and references
- PHP Generators vs Iterator objects
- Anonymous classes in C++

Advertisements