Simplify Your PHP Code With the PHP 8 Class Constructor Promotion: The Wonderful PHP 8 Features and Changes Part 5 - 9 Minutes Lately in PHP Podcast Episode 88

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Simplify Your PHP Cod...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2021-08-20

Posted on: 2021-08-20

Viewers: 299 (August 2021 until September 2021)

Last month viewers: 47 (September 2021)

Categories: Lately in PHP Podcast

PHP 8.0 introduces the class constructor promotion allows PHP developers to automatically set certain class variables when a new class object is created. This improvement allows developers to initialize new objects with less code.

Manuel Lemos and Samuel Adeshina comment specifically about the PHP 8 class constructor promotion in the fifth part of this podcast episode.

Listen to the podcast, watch the video with manually edited subtitles in English, read the transcript to learn more about these exciting PHP 8 topics, and find where to buy the original PHP elePHPants shown in the podcast video.




Loaded Article

Contents


Listen or download the podcast, RSS feed and subscribe in iTunes

Click on the Play button to listen now.


Download Size: 3MB Listeners: 335

Introduction music provided by http://spoti.fi/NCS

View Podcast in iTunes

In iTunes, use the Subscribe to Podcast... item of the Advanced menu, and then enter the URL above to subscribe to this podcast.

Watch the podcast video


See the Lately in PHP podcast play list on YouTube and Subscribe to this channel there.

Episode 88 Part 5 Video

Transcript of what was said in the podcast

List of Transcript Sections That You May Read Below

The PHP 8 Class Constructor Promotion (0m 17s)
How to Buy the Official PHP elePHPants (3m 11s)
Upcoming PHP Classes Marketplace (5m 55s)
Conclusion (10m 02s)

The Actual Transcript

The PHP 8 Class Constructor Promotion (0m 17s)

 Manuel Lemos:  So let's move on to the next feature. We are already close to the end but there are some more important features to talk about.

Samuel Adeshina: Yes. So we have the constructor property promotion. This is also another syntactic sugar in PHP 8. Think about how you would create a property in a class right now that you need to use within other methods within your your class.

So you would start by creating it publicly or what whatever scope you give it: public, private or protected. Or you start by creating a variable which you can call access using the this or the identity or the self within your functions, other methods and all of that.

Now with PHP 8, you can just define your function, your values, properties in the constructor class. And then they get put to the global scope.

So what it means in essence, is if you create a property a new property within the constructor you can access it using the $this operator in other functions of the class.

Basically it gets promoted globally so you can access it as you would have if you had created it in the beginning of your class. So this also helps you avoid writing duplicate code, basically repeating yourself.

One of the most important things obviously is trying not to repeat oneself. So this feature is a new addition to PHP 8 to actually avoid that.

It's very simple and straightforward. Define your property with the types and all of that and then we use it wherever you need to without having to call it from the from outside the constructor function.

Manuel Lemos: Yes, this is great. It's another step of optimizing the language to do more with less. So in this case you would need to declare the variables in the old style if you prefer to declare the variables, and you should be doing things in the best way that helps you to maintain your code.

And what you are going to have here is the declaration of the variables and the default values of the constructor, all in a few lines. So it's sort of a optimization to do more with less code.

How to Buy the Official PHP elePHPants (3m 11s)

Manuel Lemos: And we are practically ending this podcast. And it's up to you. This is just one episode of the Lately PHP podcast on which we are talking about PHP 8. We will also talk about other  aspects.

What I would like to say to end this podcast if you allow me, we are doing this podcast is also work, work of collaboration we are not asking anybody to pay us.

We are doing this because we have fun. We are doing this as a part of the content that will be published in the PHP Classes site, as well in YouTube.

And what we would like, if you allow us, is that if you would like to support not only the PHP community as a whole but also PHP Classes, is try to find these in the PHP Classes just to show...

Samuel Adeshina: The best stress relief you need as a PHP programmer.

Manuel Lemos: ...this is the official PHP ElePHPant that was allowed to be produced and sold, by Vincent Pontier from France, the creator of the design. Actually, there one improvement, like PHP 8, think about this is the PHP 7 ElePHPant. The PHP 8 is more like this.

This is the the pink, as you may see. And we are going to also make available the possibility to personalize your elephants with maybe your name, like Carolina, and oh this is the PHP Classes also. These are optional. Actually you need to do extra work to pay that cost a bit more.

And one possibility is this to have the PHP 8 logo. There this is also a design of Vincent Pontier.

We are very, very grateful for him and for his kindness. He's been supporting the site for many years. Not only he allowed us to produce this one but also the previous one which was also an improvement from a contributor of the site.

And we want actually to pay him royalties and he was being kind enough to waive those royalties. And all the money that we can get to to the site is reverted to continue further development.

Upcoming PHP Classes Marketplace (5m 55s)

We want to pay more contributors, more PHP developers to help us to improve the feature that many developers want. Oh I would like the PHP Classes to be better.

And PHP Classes will be better in a way that I hink many people will like, which is, we are going to help developers to buy and sell their own software products written in PHP or maybe APIs or components or courses, many things that will be announced soon and are part of a plan that we have been executing very slowly but is moving on every day a single bit until we get there.

So we hope in the next months we open the PHP Classes marketplace which will be like many markets that are out there on which can help you, Samuel, or other people to have their products be sold in the market.

Conclusion (7m 07s)

So basically that's it. I would like to thank you for the opportunity to listen to us. And hopefully we have provided value in terms of knowledge about the new features of PHP 8, as well features for the upcoming versions of PHP, by versions I mean the other features that PHP 8 has implemented.

And on my behalf that is all for now. I'll like to leave you with the last words from Samuel. Last words for now. He is not going to die okay?

Samuel Adeshina: Thank you for this episode about PHP 8. And I would just encourage you to check it as soon as it is released which is hopefully in less than two weeks from now. 

Manuel Lemos: OK. Let's see us again soon. If you would like to propose topics or even participate in the podcast with us, just contact us. We're going to make available the contacts.

We are very accessible and anybody can contact us. Send us emails. Send us spam. Send us complaints. Send us complaints. Send whatever you feel it's right for you to comment. So we will be waiting for your comments, or not, it's up to you. On my behalf that is all for now. Bye.

Samuel Adeshina: Thank you. Thank you. Bye.

Show notes




You need to be a registered user or login to post a comment

1,586,494 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

No comments were submitted yet.



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Simplify Your PHP Cod...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
For more information send a message to info at phpclasses dot org.