

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- PHP Anonymous classes
- PHP Anonymous functions
- Anonymous classes in PHP 7?
- Creating Instance Objects in Python
- PHP Objects.
- Creating and Using Objects using Perl
- PHP Comparing Objects
- How to implement lambda expression without creating an anonymous class in Java?
- Access variables from parent scope in anonymous PHP function
- Storing objects in PHP session
- PHP Objects and references
- Creating an array of objects based on another array of objects JavaScript
- Creating multiple Java objects by one type only
- PHP Generators vs Iterator objects
- Anonymous Methods in C#
Advertisements