
- 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
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" }
- Related Articles
- How to pass arguments to animation.FuncAnimation() in Matplotlib?
- How to pass arguments to anonymous functions in JavaScript?
- How to pass arguments in Invoke-Command in PowerShell?
- How to pass arguments by value in Python function?
- How to pass arguments by reference in Python function?
- How to pass arrays as function arguments in JavaScript?
- How to pass arguments to a Button command in Tkinter?
- How to pass arguments by reference in a Python function?
- PHP Function arguments
- Python Program to pass the tuple as function arguments
- Named Arguments in PHP 8
- Java Program to pass method call as arguments to another method
- How to pass command line arguments to a python Docker container?
- How to pass reference parameters PHP?
- How to implement a constructor reference with one or more arguments in Java?

Advertisements