Author: Atif Shahab Qureshi
Viewers: 1,699
Last month viewers: 2
Categories: PHP Tutorials
But maybe more interesting to all developers that live and breathe Object Oriented Programming every day is what is new in terms of built-in classes and interfaces.
Read this article to learn not only about new classes and interfaces added in PHP 7 but also relevant global constants.
Contents
Introduction
New Classes and Interfaces
New Global Constants
Conclusion
Introduction
PHP 7 introduced so many changes, that it was necessary to split this migration guide tutorial in several parts.
This time it will be covered the new classes, interfaces and global constants based in the official documentation of changes of PHP 7.
New Classes and Interfaces
Intl
The Intl extension is basically for internationalization. Using this extension PHP developers may perform actions like encoding text, locate text boundaries, perform date and time calendar operations, format and transliterate text.
This extension was introduced in PECL repository and in PHP 5.3 after the plans for the original PHP 6 were dropped out. In this extension, only one new thing that was introduced in PHP 7.
IntlChar
IntlChar provides access to functions which are needed for manipulating text using Unicode characters. These functions are similar to those provided by the ICU library. Along with these, a list of Predefined constants has also been introduced in PHP 7.
Reflection
The reflection API makes it possible to introspect methods, extensions, interfaces, functions to retrieve details of classes. With the Reflection API, the following two features were introduced in PHP 7.
Reflection Generator Class
This class is used for reporting about a generator. It has further classifications as below:
ReflectionGenerator::getThis
It will be used for obtaining $this value
ReflectionGenerator::getFunction
It will return the function name of the generator
ReflectionGenerator::getExecutingLine
It will return the recent executing line of generator.
ReflectionGenerator::getExecutingGenerator
It will return the executing generator object.
ReflectionGenerator::__Construct
It will construct reflection object
ReflectionGenerator::getTrace
It is used for getting trace of executing generator.
ReflectionGenerator::getExecutingFile
It will return the file-name of recently executing generator.
Reflection Type Class
This class is basically for the purpose of reporting information about return type of the function. It has following three classes:
ReflectionType::__toString
To string
ReflectionType::isBuiltin
It will check whether it is a built-in type
ReflecctionType::allowsNull
It will check null is allowed or not
Session Handling
Sessions are used in PHP for storing data related with the current user as he navigates through different pages. A user that comes on the Web site is given a session ID. It may be stored on a user client side via the cookies.
Although the concept of sessions was implemented many years ago in PHP 4, the SessionUpdateTimeStampHandlerInterface is the new thing that was introduced in PHP 7.
Exception Hierarchy
Division by zero error
DivideByZeroError will be displayed when your code makes an attempt to divide any number by 0.
ArithmeticError
This error will occur in result of any attempt to perform a bitshift by negative amount or may be due to call to indiv(), which will ultimately result in value other than the boundaries defined for the integer.
AssertionError
This type of error occurs in result of assertion through assert() failure.
Parse Error
Parse error occurs in result of parsing when function eval() is called.
TypeError
This type of error occurs in result of 3 scenarios:
1. When invalid number of arguments are passed to a PHP function
2. When declared function return type does not match the type of value returned by a function
3. When argument type passed is mismatched with declared parameter type
Error
The base class of all errors is Error. Following are the list of error classes:
Error::getCode
It will return the code of error
Error::getPrevious
It will return the last throwable
Error::getMessage
It will return the error message
Error::__construct
It is used to construct error object
Error::getTraceAsString
It will return the stack trace as string
Error::getTrace
It will return the stack trace
Error::getLine
It will return the line where error has occurred.
Error::__toString
It is used for string representation of error.
Error::__clone
It is used for cloning of error
Error::getFile
It will return that file due to which error has occurred
Throwable
It is actually the interface for object which is thrown using the throw statement, which includes exceptions and errors. You can use Throwable with the methods below:
Throwable::__tostring
It will return string representation of thrown object.
Throwable::getPrevious
It is used to return the previous throwable
Throwable::getTraceAsString
It will return the stack trace in form of string.
Throwable::getTrace
It will return the stack trace
Throwable::getLine
It will return the line where object was instantiated.
Throwable::getFile
It will return the file where exception occurred.
Throwable::getCode
It will return exception code.
Throable::getMessage
It will return the message
New Global Constants
Core Predefined Constants
This is the list of predefined core constants including the ones defined by SAPI modules, Zend Engine and PHP. The newly introduced core constant in PHP 7 is PHP_INT_MIN.
Libxml
These constants rely on libxml extensions, XML writer, XMLRPC, XML Reader, XML, XSL, WDDX, SOAP, SimpleXML, DOM. The newly introduced one was LIBXML_BIGLINES
PCRE
These are the functions which perform operations on regular expressions. The newly introduced in this list is PREG_JIT_STACKLIMIT_ERROR.
POSIX
POSIX functions are specific of operating systems that provide APIs that comply with POSIX standard.
The newly introduced constants used by POSIX functions are:
POSIX_RLIMIT_Core
POSIX_RLIMIT_DATA
POSIX_RLIMIT_LOCKS
POSIX_RLIMIT_MSGQUEUE
POSIX_RLIMIT_As, POSIX_RLIMIT_CPU
POSIX_RLIMIT_FSIZE, POSIX_RLIMIT_MEMLOCK
POSIX_RLIMIT_NICE, POSIX_RLIMIT_NPROC
POSIX_RLIMIT_RTPRIO, POSIX_RLIMIT_STGPENDING
POSIX_RLIMIT_NOFILE
POSIX_RLIMIT_RSS
POSIX_RLIMIT_RTTIME
POSIX_RLIMIT_STACK
POSIX_RLIMIT_INFINITY
POSIX_RLIMIT_STGPENDING
Zlib
This is a module which basically read and write compressed files in the gzip format.
The newly added functions in Zlib in PHP 7 are:
ZLIB_SYNC_FLUSH
ZLIB_PARTIAL_FLUSH
ZLIB_NO_FLUSH
ZLIB_FULL_FLUSH
ZLIB_FINISH.
Conclusion
In this part, we have discussed new classes, interfaces and constants introduced in PHP7. In the next part, which will be the final part in the series, I will be discussing the changes in SAPI modules (interfaces with Web servers and command line) and the removed extensions.
For now you can now easily work around your PHP 7 on the Managed PHP Hosting of Cloudways, with the 1-click install. Set it up and start testing it on the most optimized cloud infrastructures.
If you like this article please share it. If you have any questions about classes, interfaces and constants in PHP 7, post a comment here.
You need to be a registered user or login to post a comment
1,616,107 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
No comments were submitted yet.