- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass arguments from array in PHP to constructor
The Reflection API can be used to pass arguments from array to constructor.
ReflectionClass::newInstanceArgs
The above line creates a new class instance from given arguments −
public ReflectionClass::newInstanceArgs ([ array $args ] ) : object
It creates a new instance of the class when the arguments are passed to the constructor. Here, args refers to the arguments that need to be passed to the class constructor.
Example
<?php $my_class = new ReflectionClass('ReflectionFunction'); $my_instance = $my_class->newInstanceArgs(array('substr')); var_dump($my_instance); ?>
Output
This will produce the following output −
object(ReflectionFunction)#2 (1) { ["name"]=> string(6) "substr" }
Advertisements