- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Group Use declarations in PHP 7
In PHP 7,Group Use declaration is more readable and can be used to import classes, constants, and functions easily from the same namespace.
Group Use declaration is used to import multiple structures easily from a namespace and cuts a good level of volubility in most cases. It is also useful to identify the multiple imported entities which belong to the same module.
Example 1
The following example shows the code before PHP 7 −
<?php use com\India\ClassX; use com\India\ClassY; use com\India\ClassZ as Z; use function com\India\fn_x; use function com\Indiat\fn_y; use function com\India\fn_z; use const com\India\ConstX; use const com\India\ConstY; use const com\India\ConstZ; ?>
Example 2
The following example shows the code for PHP7 or PHP 7+
use com\India\{ClassX, ClassY, ClassZ as Z}; use function com\India\{fn_x,fn_y, fn_z}; use const com\India\{ConstX, ConstY, ConstZ};
Explanation
In Example 1, we used PHP statements for specific classes, functions, and constants in a namespace and also used many duplicate lines for each class, function, and constant with lots of use statements at the top of the file which is not good.
Example 2 shows the equivalent code in PHP 7 where we are using multiple class, function, and constants within one line
Note: To overcome the multiple-use statements and classes PHP 7 added a new feature called group use declaration.
- Related Articles
- Types of Group Use declarations in PHP 7
- Use Declarations in Rust Programming
- Why do Java array declarations use curly brackets?
- Preg_replace_callback_array() in PHP 7
- Array Declarations in Java
- Array Declarations in C#
- Migrating PHP 5.x to PHP 7 on CentOS 7
- Anonymous classes in PHP 7?
- Generator delegation in PHP 7
- Type Hinting in PHP 7
- Constant arrays in PHP 7
- What are JSP declarations? In how many ways we can write JSP declarations?
- What are Declarations?
- Generator Return Expressions in PHP 7
- Exceptions and Error in PHP 7
