![]() |
|
[PHP] Summary of PHP 7 features - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183) +---- Thread: [PHP] Summary of PHP 7 features (/showthread.php?tid=5131775) |
Summary of PHP 7 features - Sakuto - Jan 12, 2016 Summary of PHP 7 features Hello everybody, as you all know, PHP 7 is finally available in every good hosting service, and it's for this reason that we will take a look at the major features of this new version. I will try to explain everything in a clear way with example, and I hope you will enjoy reading it :) [img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Scalar type PHP 7 introduce a whole new feature, as you were already able to do it before with class, you are now able to use scalar type to define the expected type for parameter, but also the return type of a function. Here's a quick example:PHP Code You have two level of verification for scalar type, strict mode and non-strict. In the non-strict mode (the default mode), PHP will try to convert the given variable into the right type with a boxing system. Contrariwise, in strict mode, if the given variable doesn't match the type, PHP will return a Fatal Error. For example, if we are taking back our previous code, the second call of our function would make the script return a Fatal Error. You can easily switch from one to the other by typing this in the beginning of your page. PHP Code But what would've been scalar type without return type? Nothing really interesting. You are now, in addition, able to define a return type for your function, let's take our previous code again and implement it! PHP Code PHP actually support eight primitive type:
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Null coalescing Operator Great addition to this version, the null coalescing Operator, this simple operator will allow you to drasticaly reduce your code! It will return take the first parameter if it work, otherwise the second. Here's an example:PHP Code [img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Tie Fighter Operator New little feature that will allow you to compare two variables. It will return:- 0: if both variables are identical - -1: if the first variable is the greatest - 1: if the second variable is the greatest PHP Code Very simple addition, but could be useful and allow you to avoid multiple condition with if, elseif, else... [img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Array constants You are now able to declare array in constant, nothing really hard to explain here so let me quickly show you an example:PHP Code [img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Group use declaration The new version give us the ability to group multiple use in one instruction, particulary useful when working with Framework or a lot of library.PHP Code [img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Closure Call We're now able to inject closure into existing class with the method ->call(). This method will be executed as a method of this class and so will have access to the context of the current class instance. PHP Code A bit of explanation for this piece of code. We are creating a new class with a public attribute firstVar. Instantiate and assign a value to your instance of class myClass. We are then creating a closure that just return the variable firstVar. When calling the closure on the foo instance, the current context will be equals to the context, the closure will get access to the attribute firstVar and will correctly return "bar"; [img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Assert The assert function is fully changed, in the last version of PHP, we were only able to return a string as the second parameter. In the version 7, you can now return custom class to avoid typing again and again the same message, a short example to make it clearer.PHP Code The main advantage is that you don't need to write again and again the same message, you can now return any class that implement the Throwable interface. Making it easier to test your script. [img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Mysql_* is dead To conclude this short presentation of the new feature, we will talk about the best decision made on this release. The suppression of mysql_* function.You are now forced to switch to PDO or MySQLi, and it make me feel good, really good. [img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
And way more... Of course this list was not exhaustive, they are a lot of new feature / addition to current feature to explore like Yield, Anonymous Class, Reflection and much more... But it's up to you read to this if you're interested. I tried to make a short list of the features that I think, you'll use very soon.Thanks to Haxalot for the reference guide linked in Yield, Anonymous class and Reflection
* - Nipsey Hussle - Jan 12, 2016 Nice share man! Really like the features php 7 has brought on make things easier and other nice features they've added! Namespace short cut is a feature I will be using a ton most the time when I'm working with frameworks or packages with composer which I do a ton! Great thread to help anyone also trying to conver code from 5 to 7 if they want too :) * - Alex™ - Jan 12, 2016 PHP Code * - LilDurk - Jan 12, 2016 Nice share Sakuto, always like your threads. * - Fkn - Jan 12, 2016 Great thread Sakuto, thanks for the time you've put into this. * - ShizzleFly - Jan 13, 2016 Great thread here buddy. +repped for making these threads * - Hash G. - Jan 13, 2016 Great and informative thread Sakuto, thanks. * - newQuery - Jan 13, 2016 Awesome man, very useful thank you ! :) * - FocalBanger - Jan 13, 2016 Thanks for the share man! Very useful and well written! * - SIM-2434055 - Jan 13, 2016 Nice job on the thread! 👍 * - $lick - Jan 13, 2016 (Jan 12, 2016 07:45 PM)Alex™ Wrote: Fucking finally, it was killing me to not be able to do this. * - noname1 - Jan 13, 2016 nice write up Sakuto, thank you * - Fox™ - Jan 13, 2016 mysql_* has been deprecated since 5.6 though. * - Sakuto - Jan 13, 2016 (Jan 13, 2016 02:39 PM)Fox™ Wrote: mysql_* has been deprecated since 5.6 though. It's not just deprecated, it's fully removed now. Thanks everybody for your kind comments :) * - Fox™ - Jan 13, 2016 (Jan 13, 2016 02:41 PM)Sakuto Wrote: It's not just deprecated, it's fully removed now. mysql_* functions for me didn't work in 5.6 anyway when I was trying out a really old script that I wrote, but if they weren't removed fully then, I'm glad it is now. |