 good afternoon and welcome back guys. Have you guys heard about PSR? So that's what you are going to hear about. Do you guys use PHP? Do you have any favorite framework? Okay. Anything else? Okay. So any other framework that you like and use? Okay. There are so many frameworks in PHP, right? How to get interoperability between the frameworks? If you are in one camp and never switched, you may not have. But if you have to move between the frameworks and you like some component of a framework and you missed out in the new framework that you are using, you will feel the pain. That is where PSRs come in. They come up with some recommendations to how to make components interoperable. So you can take a component from one framework and you will be able to use it in another. So let us welcome Zeon to talk about that. Hi, my name is Zeon. Today, this social will be on understanding and implementing PSR. Some housekeeping. The Wi-Fi SSID and the password is on the wall. The one on the right is the SSID. There will be some short, hopefully short lab practices, some hands-on. So you will need PHP more or equals to 5.5 preferably 7. You will need Composer as well. A web server is optional because as for PHP 5.4, it has an in-built web server. You can just run from command line, PHP space minus s, space localhost and your pod. And if you have forgotten to bring your laptop for a workshop, you can use pen and paper. So if you have installed PHP, which I highly doubt, since you are here for PHP conference, there are some notes that you can find out how to install it. Bit.ly.y.zm-psr. Okay, a little bit about myself. As I said, my name is Zeon. I was formerly a freelance web developer for two years. I have been programming since 14. I pick up PHP around the year 2000. Let us just check the website. This is my personal website. I am a Zen certified engineer as well in PHP as well as Zen framework. So if you want to contact me, you can come to my website and try to click on the two-dimension. Okay, our workshop agenda for today. What is PSR? PSR 0, auto-loading standard, PSR 1, 2, 4, which is also another auto-loading standard. And finally, PSR 7, the HTTP message interface. Okay, so first thing, what is PSR? It stands for PHP standards recommendation. So it was proposed by the PHP framework interoperability group, FIG for short. Any guesses on how this FIG came about? People argue. Okay, it was a PHP conference just like this. It was at the PHP Tech conference in 2009. So these representatives from each major framework, they came together and said, see, we are all doing our own stuff. And then when someone learns Symphony framework, they go to another company, which is using a Zen framework, they do relearn everything again. Why not we try to find the common points so that we can work well with one another and most importantly, improve the employability of PHP developers so that it's easier for PHP developers to find jobs everywhere. So right now, there's about 40 plus voting members as of now from various projects like Composer, Drupal, Magento, Pear, Zen framework, Larevel and Symphony framework. This project adopts the standards others are welcome to. You are welcome to, I'm welcome to, but it's not compulsory. Hence the word recommendations. Okay, I'm sure this cartoon is quite familiar. Now PHP being easy to pick up it sees many different types of developers with different quality skill level, many different type of design factors, many type of implementations for the same type of problem. This has led to some impressions. Some are that PHP is no good. Sometimes you see some security penetration tester here. This website is done in PHP, not Java. I think a lot of vulnerabilities. That's because there are too many varying standards. But the thing, the fact is PHP powers over 82% of the websites in the world. So we do have a use case for ourselves. So projects who join FIG, as voting members, they do so because they want developers who use and contribute to their projects to use these standards to be exposed to best practices and to help shape the future. As you go through the PSL, sometimes you will see these words, must, must not, required, shall, shall not, should, should not, recommended, may, optional. A lot of documents are like those documents drafted by the ITF, Internet Engineering Task Force, how your SNTP, your email runs, how your DNS server runs, all these are implemented based on RFC. So a lot of them they use this terminology to say this should be done for it to be operable, compatible with everyone. This is recommended but not compulsory. So with that, we start with the first one. In the very beginning, this is how I started 16 years ago. We missed everything together. So your database circle will be here at the top. It's pure procedure programming. We will check our super global posts for a summit. We will run an SQL statement and we will render the HTML over here. So this in short is spaghetti code. You are missing everything together. Let's say if one day I want to change the HTML or change the variable, I need to go to thousands of files to change it. It's all too tightly coupled together. Later on, we started using include files with common functions. So instead of having duplicates of the same type of code everywhere, we extract them up to different include files. Now, one question. Anyone knows which folders or which directories include users to find the files? Like. Like. That's a library folder. Okay. That's a library folder. But how does he find the library in the first place? Through the PHP init file. Okay. PHP init file. He is not the official answer. Other people can answer as well. The first place that he finds will actually be the current folder, obviously, and all the subfolders. And let me see whether I can change this. Okay. Laila, what? Who do you want to see? There's an include path in your PHP.init where you can actually put which are the, which is the path of the directories that you want include or require, include ones that require ones to actually find when you try to include a file. Okay. One problem with this include file is you find that actually it's not invalid. So I find that, let's see if I start to use include a file written by someone else, there is a very high possibility of name collision. When we call his function, it's not invalid. And global functions is kind of something that should be avoided. So with PHP 5, 5.3 especially, they started including classes and namespaces. So a simple class, a namespace. Namespace say that I'm using a library from Zen. So namespace Zen, I'm using a library from Amazon. But as it came out of SDK, so it would be namespace Amazon. So it helps to segregate. Say I can have a namespace Amazon class bar. I can have a namespace Zen. I have a class bar as well. So from common files to common libraries of files, instead of include statements everywhere, we started using namespaces. So with namespaces, the thing is where are the include statements? How does PHP know actually where to include it? So auto-loading, the title there, auto-loading standard. Auto-loading is where you specify a name, space, or the class. In this case, useful bar, when I instantiate the bar variable, you'll know that, OK, I'm looking for the second class over there. How we know where to find it? So this is where auto-loading comes in. And the most basic is actually you can write a function to tell PHP, OK, you give me a class name, I will retrieve the contents and include that file for you. So how to register it? SPL underscore, auto-load underscore, register the name of the function. OK, I'll go through a few of the things. This is plug directly from PSR0. So what is needed? What is required? If you want to comply with PSR0, what do you need? First thing, a fully qualified namespace and class must have the following structure. Slash vendor name, OK, not package name, slash vendor name, let's say Amazon, Zen, any amount of namespaces, and then finally ending up in the class space. So for example, you can have full. Full will actually be the actual vendor name. And you can have as many subspaces as you have in the middle and ending up with the class name. OK, each namespace separator is converted to a directory separator. This is a PHP constant. So in Windows, you have your backslash, which is like this, backslash because it's leaning back. And in Units and Mac, it's a forward slash. And in the class name, the part that is at the end of the namespace, the class name, the name of the class, every underscore letter is converted to a directory separator. OK, it has no special meaning. OK, because this underscore was something brought over from 5.2 where they didn't have namespaces yet. So they came up with their own way to enforce a namespace. And finally, the fully qualified namespace and class is suffixed with .php when loading from the file system. So I have a full slash bar. OK, I'll say, OK, I supposed to load full slash bar .php. You can have any combination of alphabetic characters, can be lowercase or uppercase. So for some examples, in those who use .tring, it's a library for a database of ORM. If you use a class slash .tring slash common slash isolated class loader, they will load the following. They'll find the correct project library. And then the rest is the same. But you convert the backslash to a forward slash and append .php to it. Same thing for symphony library. For the Zen ACL as well. Now, just now I referred to the underscore. So class name, your class name has underscore. So your auto loader, when you look for it, if you are going to comply with this standard, it should be loading class slash name .php. There is a very subtle difference we should find out later. This only applies to the class name, not the package name, not the things in between. So we find that for package name, package underscore name, it is still written. Only underscores in the class name are converted. So we are going to do a short lab practice. I will be asking you to download a zip file. You can open it up. And the result is supposed to, you will get a file like this. Later on, when you download a zip file, you'll find that there's quite a few files here. This is our index.php. Okay, let me see. Okay, you'll find a few files over here. This will be the result of our index.php. And when I run it, when I run it, so basically you will write autoload.php. You'll write. After that, I will try to call a few classes which have been included in the folders. And I'll just see whether you are able to load the file. Let you have a practice. Okay, let's say if I were to run this. Oops. That is the R. Let me see if I can bring my mouse over. Okay. So later on when you run your, after you finish writing autoload.php, when you just call it, you run the index.php, you should be getting these four answers. So it shows that you are able to load the file. Is it bigger? Can you see? Just four simple lines. Okay. So this will be the contents of autoload.php. Basically, you write, you are given a class name. Let's say full. So you must resolve it to full.php. If I give you full slash bar, what would be the correct name to load? Okay. You can download the file from here. bit.ly-zn-psr0 Where is my mouse? We will look at about 10 to 15 minutes. Don't worry if you are not able to finish it on time. Most importantly, this workshop is for you to actually appreciate what PSR is. Right now you have a lot of book camps. A lot of people, a lot of students, start with high level frameworks, but they don't know what is happening at the bottom. PSR actually enables, as what Aru has mentioned just now, to actually enable frameworks to work together. You can mix and match. All because of PSR, they are all adhering to these standards. So it allows us to actually cooperate well with one another. It's all well to use frameworks for rapid development. But when a problem hits the fan, you need to know your basics and what is happening. The first three examples will look quite easily. The last one is a bit tricky. Just remember what I mentioned about the package name and the class name. For those who are running Apache or XAMP or MAMP, you can just run it from our browser. For those who have PHP 5.4 and above, you can just run this from the command line. In the same directory where you downloaded the file. So after that, you can just go to localhost colon 8080 and the file will run as per normal. It's a capital S. Do we have elephants? It's here. It's his own. What is the command term in PHP localhost? PHP hyphen capital S. Space localhost colon 8080. Then you call your browser. It's the last one. Our role over here has gotten the first three outputs already. Anyone else has gotten the first three outputs? This one, you are just replacing the slash and underscore. In order to cater for the fourth one, you actually need to differentiate which is the namespace portion and the class name portion. Only class portion you can replace? Yes, only class portion can replace. Those who have gotten the first three outputs, most likely you just did a simple replace of the slash and the underscore for your whole class name. But in order to cater for the fourth output, to get the fourth output correct, you need to differentiate which is the namespace portion, namespace plus subnamespace, and which is the actual class name. Only underscores in the actual class name should be changed to your directory separator. Those in the subnamespace, your package name should not be touched. We will just give a minute or so before I so-called review the answer. Actually, the answer is on the PSR website. Now, with this autoload, this standard wide enables is XAM framework can write the libraries, can write the classes, Symphony framework can write the classes. You can come and have your own mini framework. So as long as you comply with this, one person just need to write one autoloader once. So let's say I write my own autoloader. And this autoloader complies with PSR0. I know that XAM framework, LRFL framework, Symphony framework, KPHP, Co-initers, they comply, they write the classes to be compatible with PSR0. So my autoloader will be able to autoload all of the classes. So I like this particular class in Symphony I pick. I like this particular class in XAM framework, probably authentication I pick. I like this particular class in .3OM I pick. So my autoloader, which I wrote myself, according to the rules over here, will be able to autoload all of them and include the files in my project. So that is a very important step. This allows us to mix and match. I will show the answer now. Oh, yes. Okay, let's see. Okay. So first thing, this is your store. I did not mention this. Usually sometimes class names, for absolute, fully qualified namespaces, they put a slash in front. But this one we want to ignore. So we just trim it off. So as I said, we need to differentiate between the namespace and the actual class name. So over here, first, we find that if there's a, we look for the last slash. Last slash in the class name, and we split it up. So supposing let's say if you have, if you have full slash bar. So you find that full becomes the namespace and bar becomes the class name. If I have full bus bus, the full slash bar becomes the namespace and bus becomes the class name. And over here, what I did was, we found name, okay, basically only underscores in the class name are converted to slash, the entry separator. The namespace is not touched. So if you run this, you find that the last example, which loads, let me see, which loads full slash my package underscore name slash my class underscore name. This should be retained. This will be converted to a slash, the entry separator and dot PHP. So it becomes a inside vendor slash full slash my package underscore name slash my class slash name dot PHP. Okay. Okay, with that, let me go on to PSL one basic coding standards. Okay. This is a workshop, two and a half hours workshop. There is not enough time to let everyone have half an hour on trial. So I try to keep the lab practices a bit of hands on, but enough for you to get a feel of it. Most importantly is to pick up your interest. So after this workshop, you forget everything, you forget my face, it's okay. As long as you're interested to go back to find out more about PSL and even better contribute to PSL. We will have a speaker from Symphony Framework. If I'm not wrong, he is currently the secretary of PSL. So I'm looking forward to his talk. I hope you are too. So method names. The one of the most hardest thing in programming is naming. So how would you name a full bar? Would you name it fully lowercase full underscore bar? Workplace, let's do this. This is what we call a camel case because the capital B, there's a hum there. So it's called a camel case. Center's case are central style. Full bar, which is what we call sturdily caps or Pascal case. Those who use turbo Pascal before. Or even better, everything caps. I know your gate half-handed is all capital. So imagine standards are not high and fast. You can change that as long as you are consistent. Like for WordPress, WordPress doesn't use PSL, which is fine. I like WordPress. I use WordPress myself. Their coding style is everything. All the method names will have underscore, which is fine as long as it's consistent everywhere. Please don't have something of each example of here. So imagine a company's code base with no naming conventions, which is quite easy. You have a turnover every year. Every year your developers leave new developers come. So if there are no naming conventions defined, it's going to be chaos. So we just go through a few things. For PSR1, first must always use angle bracket, question mark, PHP, or angle bracket, question mark equals. Now, if I remember correctly, when I tried it 16 years ago, there was a possibility where you could use angle bracket percentage. You could use it with .asp files also, not your asp.net, the O, O, ASP. So, yeah. Files must only use UTF-A, Revolve BOM for PHP code. BOM is basically byte or the mark. Okay, it is a unique code character put at the start to indicate the the ambient, the type of encoding. This is more applicable to, let's say, UTF-16, where you will matter more. But for UTF-A, it doesn't really matter. Now, omitting the BOM allows older applications. Those test editors that don't support, they are not unique code aware like windows, no pack, yeah. You allow them to be able to read your PHP code. At least, you can see the words. Files should either declare symbols or cause effects, but should not do both. Should not is recommended. It's not compulsory. So, let's see if I'm just writing a small test script. I just want to write for fun, right? I can mix and match. I won't be fine or the PSL committee coming up. Should not is recommended, not to. But if you really have no choice, yes, probably. So, as an example over here, I hope it's kind of dead. See this? Yeah, in front. Okay, now this is an example to avoid any set. You are changing your any settings. This is a side effect. Include a file. It loads a file. This is a side effect. Echo HTML, echo output. This is generating output. This is a side effect. Now I create a method called full. This is called declaration. So, this is an example to avoid. You should not mix the two together. Either you have a file purely for declaring a class or functions and then you have enough file for executing a code. Now, name spaces and classes must follow an auto-loading standard PSR. PSR 0 or PSR 4. I will talk about 4 later. This means that, yeah, to work on that. So, PSR 1 actually based on top of PSR 0. Class names must be declared in study caps. That means start of a capital letter and every subsequent word will start of a capital letter. Now, code written for PHP 5.3 later which you shouldn't be using. You should be going on 7 a day. Should be using namespace, proper namespaces which is like vendor slash model. If you are unfortunate to actually inherit legacy code, you will find that underscore is frequently used as the namespace separator. Class constants must be declared or uppercase with underscore character. So, supposing you can have... You can't turn the camera. No need, no need. All capital letters. Method names must be declared, camel case, camel case, not study caps, camel case with a hum. This is an example. So, for example, if you have a word date approved, you can separate the words with underscore but it must be all caps. And this will be how you write your method names, starting with small letters and every subsequent word will have a capital letter. And that's all for PSR1. Very short, right? Four slides, right? So what's the cache? The cache is PSR2. Okay. That was coding standard, this coding style. Everyone has a different style. It extends PSR1. There was a lot of opinions. There were 22 voting members so everyone had to vote. And there are also survey results available. You tell you what went on, who disagree on what, who agree on what. I will show you the survey results later. Now, this example encompasses some of the rules as a quick overview instead of me talking. So, why don't we go one round and guess the rules. Now, you first. Guess the PSR2 using this as an example. Four spaces. Very good. It's still my show. Four spaces is not very obvious here. So for indentation, four spaces. Some people like to use tabs. Some people like to use two spaces. When I started programming in C I used three spaces. I can't remember why. The thing is a lot of these projects they use version control. They commit to GitHub, SVN. The thing is if you use tabs every once editor may use a different width. But space is always a space. Microsoft Word default tab spacing is 1.25 cm or 1 inch. I use 1 cm. He doesn't use Microsoft Word. Some people may use 2 cm. So everyone have different standards but a space is always exactly a space. And it helps to say if you want to use spaces so you don't pollute your deep history. Okay. How about you, my dear friend? Would you like to guess why not be guessed from this line? Probably it would be easier. Could you think of a rule? Could you think of a PSL2 rule that would affect the coding style for this line? And yes, you can of course refer to the PSL2 website. Is that recommended but I think it's not compulsory I think. Not compulsory. No, no, actually no. Okay. Mr. Justin, curly bracket next line. Now this is for classes and methods. Some people they like to put the curly brace on the same line but after a big vote we standardize curly braces next line for class and for methods. Anyone else? There should be a space if we start curly braces it is. Okay. Very good. Okay. We are talking about this space here, right? Yes, correct. Very good. Some people don't like to put a space. Some people like to put a space. So they say all standardize. Why standardize? Because we want to improve the employability of PHP developers. You go into a new company and they say can you please look at this legacy code and say no standard of the spaces are all over so right now we are making life easier for everyone so that when we switch jobs we do switch jobs. It will be easier for us. Okay. Another thing is okay. This first line. Very simple question. Extend first or implement first? Who say extend first? Hands up. Who say implement first? Who say doesn't matter? Okay. Anyone can tell me now you answer this. Anyone can tell me why extends first? Is that any particular reason or just because it's because of the people voting? Ah, yes. Very good. Because in PHP I'm not very familiar with the other languages for PHP is a single inheritance model. You can only have one parent. So you can only have full extends bar. You can say full extends bar and codes. Don't have. So yeah implement. You can implement multiple interfaces. Now how about whoa. Whoops. Wait ah. Answer. Thanks. So all the answer already. No, that's not the answer. That is the those are the survey results. Visibility. Okay. How about this it goes now. So basically giving a default value. So in this case right B is actually optional. When I call sample function, I can just passing one function or one argument. I do not need to always passing two. So any idea why I highlighted this? Ah, optioners are always at the end. They come on. Don't always answer. I thought you're supposed to be hosting the next room. Yeah. Okay. Okay. Let's how about it's okay. It's okay. You can start. Okay. Visibility. Our dear video recorder here. Visibility final public static. Can it be public static final or static public final? So which one? Which one first? Final always comes first. Okay. Final. That's another one called abstract. So abstract and final always comes first. Followed by your visibility then followed by static. Okay. Let us look at this. Who was the one who mentioned what the space does now? Okay. The space also applies to this. Okay. So we even the space inside these brackets is important. No space after the opening bracket. No space before the opening bracket. Okay. It's like teaching you how to write your English composition. Two finger spacing then you start a paragraph. Everything must be exact. No such thing. Space between operators. If you are calling a method now some things they like to be some things they like to be explicit. Okay. One space after the calmer. No space before the calmer. You will see a lot of code where there's many, many changes and some people like to do this. So they say okay let's make our lives easier. We are always looking at screens at 2am in the middle of the morning. So let's not make our life harder. So in this case we are standardizing the coding style. Everyone will write the roughly the same way. Okay. Now. Okay. The thing that I lost users now these are the survey results. Now basically there were 22 voters 7 sections about 20 rules over here. They so all these come from the 22 voters. So for example the first one indent underscore type. What kind of indentation do you want to use? So 7 people say tabs. 1 person say 2 spaces and then 4 people say sorry sorry sorry. 1 person say 2 spaces. 14 people say 4 spaces. So who want majority way. So now we have 4 spaces for indentation. The next one line length limit soft. They are saying that you are recommended to keep your lines within how many characters. Recommended. So same thing don't know. They cannot make out of mind 2 people. Don't want any limit 3 people. 75 4 people and then the winner was 6 people just a simple majority chose 80 characters. So you are recommended recommended not compulsory recommended to keep your lines within 80 characters. Hard limit no hard limit although in the PSL2 they would recommend that you keep it even most frameworks like Zen framework try to keep your code within 120 characters. It's easier to read but your comments spend more than 120 is fine but the actual PHP code. Class name. Don't know. I don't know who is this person who don't know. Lower case snake case and finally overwhelming majority use study case. That means Pascal case start with a capital letter every subsequent word use a capital letter class brace line next line 16 people. Same line 6 people. Constance whoa they had no different opinions. Now true false now lower 19 upper 3. So what is this talking about basically is do you type it like this or do you type it like this standardized or small letters method names camera case method brace line should a curly brace for the method be on the same line or on the next line control control statements your if else statement your follow statement your while statement your do all statement same line okay I struggled with this personally for a very long time because I learn Pascal so we always had we have our begin and so I was very used to having my so called opening brace on the next line so it took me very long very long very big struggle for me actually finally to the same line so it's recommended but it's not high fast so not everyone agree but if you want to comply you will have to follow the standard always use control braces what is this talking about what if you have only one line I used to do this a lot okay if I only have one line one statement line I wouldn't care never mind just one line okay but I say that to avoid accident make it clear so everything must always be you must always use curly brace no qualms about that if else else if next line or the same line same line same line same what as the closing brace actually so you have instead of doing this okay you have this same line so this is what they mean by same line not the next line case in then when you have a case statement how many in dance how many levels are in dance not how many spaces how many level in dance we already established one level in dance is four spaces so we are saying that you have your switch statement okay you have a switch statement and your case should be one level in dance and then your subsequent your subsequent statements here should be two level in dance from the switch function space after do one do they need a function space after I say when you call a method do you actually do this okay so they say no do one when you call a method known as space a closing PHP basically if your file only contains PHP code do you close the PHP tag you don't this is so that when you include the file you don't accidentally include a blank line especially when you are using it for HTML output blank line after PHP if yours is a pure PHP file it will be a blank line as the last line and just now our video recorders talking about study of visibility first and line and dance now I think predominantly most of them are working on linux, ubuntu, mac so basically they choose unit and dance so this is not very obvious so your line and dance is basically referring to this so windows is slash r slash n but for units basically just slash n so basically you say that all your line and dance when you type your file it should end with a slash n not a slash r slash n you can read more on the survey legend now this is the lab practice okay please download this file at bit.ly slash zn-psr2 basically you are going to have a file check given a PHP file check that it complies with psr2 for each error output a line number and simple error message you can use a short course in the survey results let me show you a sample when you download the file it will look like this I have done an example for you and don't worry please don't do all of the psr2 rules you can choose so basically this is where I do it now you can use a tokenizer this is just a simple lab practice so we do it simple and basic way we will read each line okay so I will read the file name to a line I will go through the lines I will get a line number I will get a trim line so for example how do I check for tabs then I will output the line number say okay at line 5 tabs are not allowed but you use tabs so how do I detect if the line contains an if statement so after I trim the line I trim all the beginning spaces I will check if I can see an if so that means it is an if statement so what are the things psr rules that I need to check for psr check for if statement is a curly brace I need to check whether there is a curly brace if don't have most likely it is on the next line okay and when you run it okay let's see oops okay initially when you run it these are the results line 26 tabs found it is not very obvious line 30 at line 37 now to make it simpler I don't have 5 hours to do this we are going to pick and choose at first I will think probably this column over here you can pick use pick and choose from here this column over here you pick from the second column and then the group of friends sitting just in front of the window that might break down anytime you can pick the other column okay so let me write down the link again it's bit dot l y slash z n iphone ps r 2 okay we will do this for about 10 to 15 minutes our speaker just now in the morning he talk about code link thing so this will give you experience on actually yeah it's very nice for us to use right but actually it's not that nice to write so you give your sense like okay how difficult or how easy it is to write a code link test by someone of course they do have I think some of the id is actually from her I want this standard he used to beam like he used to beam no they don't have there are two types one is CS fixer and symphony which I don't like CS fixer the other one is the which is better to release up for you I'm done and the one is command line yeah I like and actually I use it on my channel also so every time I call me I actually do an automatic chat no Travis we're actually oh Travis so he said he said that's probably a CI yeah but one of the issues yeah he said like and I love the I love the list so there's no there's a few like standard standard like for people usually they're like previously one like first sometimes it's just you know like it just confuses everything together yeah yeah I know so it helps to have code for that person because I do like which of these channels your job C++ Pascal I don't think I do Pascal anymore yeah I don't do it the last time I did Pascal was visual performance pro yeah I did have some that was the last time you see they actually Pascal and Pascal and the time visual performance first you're my age you're my age you're my age so oh so much more than oh my god oh yeah yeah we are all the same generation we are all the same generation we learn what is the same oh okay visual pro came out visual basic oh good we Java Java 1.2 I forgot Java 1.9 one code and Pascal if anyone has any problems understanding what these particular learning means there's a link there there's a php-fig website where you can read out on psr2 I've deliberately only shown the survey results not the full there will take us under tomorrow the last the last one the last one is okay hold up everyone not dying of boredom yet so right now so how's the experience writing your own code linter especially since there are so many rules if you have only chosen more than 2 rules you probably take 5 minutes reading if you want a more extensive one you need to look up for edge cases there are people this has been around for some time since it started in 2009 there are some people who have already written code linters out there so some of them are going to show you an example of one and how to actually integrate it with your github project how to automatically check it instead of you manually doing it so I using windows I'm not using a Mac there is there is a php code sniffer I will show you the link later basically it is just one file a php archive file phpcs.file so what I can do is I can actually run it on my php file or a directory and it will show me all the linking problems all the code problems so right now I will try to do this php okay let me see if I can it found 44 errors so just a very simple command line php I run the php archive file and I pass in the file name and it found 31 errors and one warning affecting the linking lines so line number 1 error end of line character is invalid expected line feed file cache return and line feed now to make it easier I've exported it to to a file let's see okay that's it oh okay this is quite bad okay sorry for the uh wait a minute yeah wait a minute yeah no bother okay let's just try one uh so actually for line 1 which is over here actually I have inserted I use windows notepad to type out this file so actually there was there is a slash r slash n over here okay let's look at let's look at line 3 opening brace of a class must be on the line okay wrong this is the error so actually by right this should be on the next line line number 4 class constants must be uppercase expected pi they even spell out the correct spelling for you so in this case right in sample.bhp which is the file that we were supposed to check it's supposed to be capital PI not small letter PI now line 14 yeah 14 line entered incorrectly expected at least 12 indentations so why because this is one level for 8 the case statement is supposed to be one level indentation from the switch statement so it's supposed to be like this and then your subsequent lines like this for this indentation let's look line 25 do you tell you sorry you are supposed to name it as bus codes not bus small case underscore codes so let's look at some more line 34 there must be a single space between the closing parenthesis and the opening brace of a multi-line use statement line 34 there shouldn't be a space here there shouldn't be a space here and the opening brace should be here let's look one last one line 42 expected closing brace else slash and but found closing brace slash and blah blah blah else slash and so basically what it means is your IELTS statement is supposed to be on the same line very far space okay let me show you okay Censual Labs which is the company that came out of symphony framework they have a PHP coding standards fixer what it does is actually you fix it for you but it doesn't really show you the errors which I find a bit iffy so the other popular one is PHP code sniffer is by squeeze labs you can just google for PHP code sniffer you'll be able to find it okay let me go back to the now next one is how to integrate travis ci for github repository I'm going to show you an example from my own repository okay I present framework to module so you find that we like pictures right so every time I commit every time I commit my code to github they will actually run a auto chat with our travis ci that is continuous integration of cycle of travis so they will actually pass it and then I'll be able to get the status whether my build there's something wrong or whether it's passing this travis ci can help me to run a code linkedin as well as my unit test so example let me show you it is encapsulated in this file called travis yml okay in this case PHP I think this is supposed to be compatible with what this PHP version I wrote this about 2012 I haven't really updated things so before install I will download the cs fixer from sensual labs and then I'll run composer and then I'll run my unit test and I will actually run the script it's just a terminal command verbose dry run don't fix just a dry run that means don't fix tell me if there's errors but don't fix it please I don't want anyone to anyhow commit to my github report so level is how much psr2 let's look at the travis website so travis ci basically you give permissions to keep checking your your github repository every time it detects that you have committed something there's a commit so you put the latest changes and then you run through everything that's in the travis.yml so in this case everyone is doing it here so they actually run a terminal command they have their own worker to run through a unit test and your code linkedin so this is one way your code linkedin works so all the major frameworks they do that as well symphony framework they also have something like travis.yml some people use Jenkins and with that we go to psr4 which is another auto loading standard now psr0 is actually deprecated kind of you can still use psr0 but for new projects you can still go to psr4 so this is the extension now everyone uses composer here it's something like a package manager so I say I want to use his library I want to use a component for zen framework so instead of me downloading the zip files from github and then I unzip then copy the code over I'll just use composer I define in the composer.json let me show you an example oops where is it where is it yes this is a typical composer.json file so basically here I'll just say that what are my orders what are my requirements I'll check do I have the required psp versions and you'll just download this from the packages from github so in this case I need a component for zen framework which is zen expressive for zen standard library for development purposes I need to download psp unit so when you download a new project they have a composer.json they don't commit the dependencies to github to big bucket or to gig lab or whichever version control they use they only commit this file so you download, you type composer install, you just pull everything for everywhere and then they'll maintain the dependencies for you now over here you see autoload for the older projects and frameworks you'll find psr-0 now these are the new ones they start using psr4 you'll see that this module asks for app ninspace where do I look for it which is the directory that I start looking for in so in this case source slash app let me see so now why bother psr0 is basically based on hot or pair convention under the constraints of PHP 5.12 previous the pair installer moves source files from pair packages into one central location one folder with composer doesn't matter your packages probably you have two projects and then your Zen library is in a folder by itself so instead of having duplicates one Zen library for this project you only have one location then you can call it from there so probably you have symphony in C drive Zen framework the component in D drive the component for Drupal is in E drive so instead of copying them into one central location now with composer you can actually specify different locations where your library your dependencies are held there's more on the PHP figure out on this so this would be an example of psr0 if your project is using psr0 this is an example of how your packages and how your files would be laid out in the directory you will see your vendor directory your vendor name let's say Zen Microsoft or AWS package name let's say ACL and then the source directory where you have all the source code file then if I duplicate again this is the actual name space the vendor name so you find here you have a Microsoft this doesn't matter this doesn't name me but over here you have Microsoft again then you have the same ACL again then you have a class name this psr0 and your test would be in a different directory now with psr4 basically you are trying to compare the directory structure so you can just do this vendor you can ACL so you find that the new structure is flatter so give an example so in this case your libraries can be in different directories so Acme is the Looney Tunes company the base directory the Aura or DI framework they are in the Aura web folder separate folder you are not even in the same vendor directory symphony I want to use the request for symphony framework it's inside vendor and for Zen ACL I put it under slash user slash inclusive vendor now with all these four locations because of composer I still can bring them together and still use them because of psr4 you will make my life easier so I can actually have more variation, more flexibility with my paths and this is no lab because the lab is reserved for this this will be a longer lab this is there are about 19 psr right now about 8 or 9 of them are having a proof so I am not covering all of them obviously but this is to me one of the most exciting one psr7 HTTP message interface this is the most exciting psr the future of PHP and this is not what I said credits to talk by Matthew he is the lead developer of Zen framework in Zen technologies so I just show you this is his slides this is the future of PHP he is the one who spearheaded this psr ok now the a simple procedural example of how I will read in a city variable via my query string and then I will just check it first one test.php question mark, city equals SG so I will just check a submit button under city I will get it from the super global get I have a post form that's it, now in a framework let's say Zen framework, symphony level probably this would be how you are doing you have a test controller extending the abstract action controller some controller class specific proprietary to that framework and then you call an initial action test and then you check whether it is post if not, if yes then you get the variable my test from post and instead of outputting the html you return it to a view model a view renderer, a template engine you just pass the parameters like this even the form itself you have a class for that and the form will render itself now the problem is this and the code cannot be reused the second example different frameworks, different way of doing this, this is from Zen framework, level, symphony will have a different way of doing this now can you see a similarity all of them receive a request process a request and then return a response now why border the thing is all the major frameworks out there with PSR7 PSR6 caching and the upcoming PSR11 container the aim is to subvert and to remove the need of frameworks in the future, I am not going to say I am a Zen framework developer you have a symphony, let's start we are all PHP developers we are all Abanban we are all friends every framework comes up with their own extractions of HTTP the thing is every time you switch to a framework I use Zen framework mostly, if I want to switch to a level or symphony or cake or code editor I need to relearn how to receive a request how to return a response so I cannot switch easily so usually what do they extract from posts how to access your super global server variables how to extract the request your eye for routing purposes if I go to slash city which controller do I call how do I handle file uploads how do I handle input and output streams and the returning of response some basics PHP was designed for the web so it helps to know a little bit of basics this is what happening on your internet everyday when you look to your browser and serve some website a client which is your computer you send a request to the server halfway across the group and then the server will process it and return you a response a more detailed one basically a request this is the one in red color and the green one is the response in HTTP response the request has a verb get post patch put, delete what do I want the slash let's say for this post slash login slash login is the request URI what is the path that I want HTTP 2 is coming up currently we are still using 1.1 hopefully it will come up soon and the response by the server is which HTTP protocol version am I using the number over here 401 is the status code it's already predefined and the word unauthorized is already a standardized response phrase so 401 basically means authorized 403 is forbidden every request has a specific form if you were to doing let's say terminal just pure terminal this would be how you look like the verb the path the protocol version host, which server to call and the body response also first line status code and the reason phase what's our content the haters and then the response body this is a big overview of PSL7 now PSL is basically a recommendation they are not going to tell you how are you going to pull the request they will tell you in the phases I don't care how you do it just make sure that you have get name method I don't care how you do it whether you go to symphony framework xen framework, larval framework every time you have a request object you can be certain that you have a get name method for example you don't need to find out through documentation so symphony request is a get request name method then xen framework is a get request my name method no need you just need to know get name so they actually recommend in the phases so every box here represents an interface proposed by PSL7 the green color line is extend orange color line is users so basically message uses a stream a stream can be a file an uploader file uses a stream a request and response basically is an extension of a message so they share a lot of common commonalities like user line request body response body request uses a URI which is not so simple as just one line example.com slash test there are many parts of a URI which we will find out later and one part that is very important is server request PHP stores has a lot of superglobals your superglobal server get post how to handle all this so they came up with a server request interface that extends request interface ok we go through one by one message interface every message will basically comprise of haters let's say like your cookie your accept hater content type your request saying that the data I am sending you is in JSON so I will have a content type application slash json hater my response when I come back I can tell you I return you text slash html so haters return an array index by the hater name hater values is for a single hater hater line is to get the hater as a string message get body we should return you a string interface now one thing very important is the concept of value objects or immutability ok in PHP objects are passed by reference and if you are not careful you may be passing the same object and then you do some changes here right somewhere down there deep down 15 levels down to code right and then you find that we come back and your local show why so certain things so they have done it so that they have these with methods if you call a message with hater it returns you a new message instance you can go and play around with what you like you will not affect the original message so that's why I specifically name is new message a message if I and if I were to use a with body I change the body content you will return me a new object so you can be certain that you will not modify or touch the original accidentally especially for famous where you pass your request and response all over the place next request interface so for request interface you have get method you need to check whether it's get or post URI I'll touch on that later so basically are you asking for example.com slash test or example.com slash full bar and for body you have a new stream a stream can be a file it's an abstraction so you can write this is my JSON file and how will I compose a new request after the current request with method it gives me a new instance with URI another new instance with fader another instance and with body so finally new request will be a different object form request next response what does the response have guest status code request and status code response have so for response you have your guest status code your reason phase and your get body get body gives you a stream in the phase it gives you a stream so they do mandate that you are supposed to implement to string magic method underscore underscore to string and this is how I will create a new response with a body and with a status server request in the phase this will give you your dollar underscore get your pass body pass body basically they will process your raw body string and then probably they will return you an array or a JSON called a decoded string get uploader files for the request and how about when you pass parameters via the request you are like just now we put test.php this is particular this is why we have a server request in the phase the framework implementation details are so they don't tell you how to implement it the framework will actually pass the url get the request parameters and pass inject it into the request as a request attribute so just now I have if I passing the user ID test.php slash id slash 5 they will inject it into the request as an id attribute which has a value of 5 streaming the phase this is how it looks like it's like a file pointer you can seek to a particular cursor you can rewind you can write you can read get a contents find check whether it's a end of file this is important because they are thinking that there may be some instances where you may not download the whole body of the request at one go let's say for upload a file so let's say if you are streaming some data so they decided to make it a stream and extract it out now something interesting who has worked with files before playing files the dollar underscore files can anyone tell me is this correct let's say I perform I upload some files and I dump the contents of the files super global is this correct no it's a file name okay supposing I say upload two files one is a test file one is a html file looks very nice right first file is file0.tst second file is file1.html looks very nice right this is what we expect to see right set to say actually no this is the actual return that PHP gives us instead of splitting file by file they go by the property so the name for the first file is name 0 the type for the first file is type 0 so it's totally off so there was a need to actually extract it out this is the uploader file in the face for PSL7 I will just get uploader files for request return me an array and I will just do this there are a few methods get client file name what was the file name according to what the client uploaded the media type the size the stream to get the contents and move to method so this would be the reply file0.tst file1.html html extract it out you don't need to come and bother say I need to pass this out by myself it's going to be a bit complicated I need to hackle a lot of things so they say now I have in the face nicely for you hopefully all the major frameworks will actually implement PSL7 Zen Framework has already started already and then for every PHP developer you only need to memorize one thing PSL7 next one URI interface what's in URI you have a scheme HTTP your user interface information your host like zambo.com pod number you have a path you have a query string key goes to value and you have a fragment now if you want to get this from your server super global you can but it's going to be a lot of trouble for you so what they say that instead of every framework doing their own way of getting a URI and splitting their own parts and their own method names why not we have a URI interface so to get various parts you just need to remember these methods get URI, get scheme, get host get pod, path, query, fragment and if you need to create a new URI instance with host anything that starts with wave will create a new instance so that you do not accidentally modify the objects okay I'm sure everyone is cause I run right now now so what is so exciting about this what is this called the future of PHP that practice everyone kindly download the file over here bit.ly iven psr7 and in case you are wondering yes I actually reserved the bit.ly in advance so it is psr7 when you download this file they actually there is a readme.md inside kindly read it you have the instructions on how to proceed the lab practice okay let me see okay there won't be a need to run Composer install because actually I have downloaded the packages in the zip file but I can run it as a practice you will tell you there is nothing to update because of composer.log okay you can navigate to the public file so if I were to do this in my browser right now hmm you will tell me welcome work and if I were to add a target question mark target equals to Zion you will say welcome Zion okay how is it been done let me just go through the files with you all you will find that in the source directory inside the file inside the source directory there are basically only two files hello action and welcome action so this one uses psr7 now load I am not using any framework okay I am using a kind of library but this is a plain old PHP file I am not extending anything there is no special class that I read to extend it it is only running one method the invert magic method taking three things a request a response and the next basically the next class to pass it to and I am only using these two interfaces so basically I can use all the methods that I talked about just now I get a query parameters for the request I check for target if not specified our default to work and I will just write hello target and I return a new response to next the next one will be so this is middleware yes correct and welcome action I will this one basically kind of overwrite hello action you will actually replace the word hello with welcome now so what are we supposed to do for for this lab same thing about 15 minutes add a new class app slash off action you have written auto loader today I show you the hello action and welcome action you should know where to put the class off action top PHP first thing check is there a query string parameter called secret and then that the value is equals to today's date if it fails then return the response by word if it passes it should run as usual called welcome welcome zion add the class to config. slash config.php and then you try this where this pass the hint is the off action class uses code from hello action and welcome action miss and match try to see which will actually help me get the query string parameter so if I were to run the file now let me see if I done it correct I have not passed a secret so you just tell me by word but if I were to pass a secret with today's date as per normal or if I use a round date I show the end of the month you tell me by word so that is the practice and this is the last lap practice I promise so you can start now can you show the requirement okay it's inside the readme there's a readme.md there's the requirements are inside there's a file also called fit so you are supposed to know where to put your new off action is it on the first line or in the between or on the last line let's say by word it's based on what you are saying and I think to I was trying to get the query string with get attribute it wasn't a problem it was a query yeah I don't notice a problem just a small clarification I just realized there's a problem if you provide if the secret actually feels right okay you are not supposed to use a target so let me rephrase my question if the chat feels it should return the response by heist okay it should not say by word it should not say if I pass by target equals to Zion it should not say by Zion it should just simply say by heist this is to avoid any misunderstanding so basically your off action is will not be reusing query string parameter it will not be reusing it will not be using the target parameter at all by the way those who are first time to Singapore last year at this time when they had the PHE conference there was a big heist thanks to our neighbor so everything was very heisy so let me reiterate your off action if the secret feels or is not present it should say by heist it should not say by word by Zion by target it should just say by heist okay we give another one or two minutes because as a tea break apparently waiting for us instead of starting the next session at 4 o'clock we will take a break from 4 to 4.30 as the tea break just say by heist oh oh oh okay okay okay it was okay it was supposed to say just by heist so you don't have the hello hello anymore so the final if if I don't pass in the secret or the secret is wrong you will say by heist you will not say hello by heist okay I am going to show config file first don't worry not yet just show the config file first now this is how I have put it it's right at the top now I've seen a friend I wrote here he put it at the bottom it still works it still works now the thing is off action supposedly if I was writing authentication using login, username and password first guard it shouldn't run through all my classes first then run authentication so for authentication purposes usually I want it to run first if it doesn't pass right I'll just cut shop my whole circuit I don't waste my server resources so for this reason I put it at the most top there are two ways of writing it I can put it as a string or in PHP 5.5 or newer I can just use a class keyword now for the answer okay so basically I will get the query parameters for the request I will get a secret I'll check is there a secret parameter passing if yes I'll use it if not I'll just give a blend string now in PHP 7 which I've been using a lot this can be replaced with which I I mean I've been abusing so you'll actually be as simple as this so this is another parts of switching to PHP 7 I've been waiting for this for a long time yes is it valid or not I'll check whether the sequence is equal to this string if it is not valid I will create a new stream I'll write by his and I'll return the response if not I'll pass it to the next class to run that's it a simple as that authentication so I'll go through again I take the query parameters I'll check it if the secret does not match I will create a new response and I'll return it if not I'll pass it to the next class which is hello action to deal with it and I'll just pass it request and response now so what we did just now is what we call middleware PHP 7 is a list of interfaces it doesn't look so interesting but it is exciting for the fact that it enables the concept of middleware those people have been using like no.js they would be familiar with it so with middleware it's just a black box those who take commsigns, those who study bookends you know that we always talk about black box so middleware is like black box you are just taking a request process it and give a response that's it now with this middleware you won't need to worry about symphony, bundles Zen framework Laravel packages you just need to know middleware I just need to create a class like that and that will work anywhere whether it is symphony framework or Zen framework or Laravel framework now how does it actually work let me if you have a look at there is file public slash in desktop PHP the file that you run PSR 0, PSR 4 I call it the auto loader so that every time I use a class you will just load a file for me strategy and the throws is implementation of PSR 7 that the throws is implementation of PSR 7 strategy is the middleware implementation so I create a pipe and then for each class in the config I wrote it up it's a simple case of course there will be more complicated than that in real life situation basically add a class to the pipe so middleware is hello I'm the client and the request I'll pass it down the pipe let's say this is the pipe I'll pass the request and the response object objects are passed by reference right request and response object I'll pass it to the first guy he process it he probably inject something to the request modify it or inject something to the response modify it you pass it to the next guy next guy I do do I just inject some things probably I put in a logger probably I check for ACR authorization then I pass it to the next guy the next guy say ok candy I think this is enough I'm the final middleware so ok I calculate the secret for you then he return back to this person and then comes up back to the client so this is the concept of middleware today let's say I doing hello and welcome now I want to add authentication I just wear middleware authentication no classes to withstand no classes to withstand just a simple magic invoke and the same signature taking a request response next I return the response or I pass it to the next middleware so tomorrow I want to add authorization I write an ACR action I insert it in between I want to do some logging so do I go to every action let's say I got 1000 actions do I go to every action type logger, call, log then this message or I have the request and the response ready everything is inside why not I just write a middleware called log action and just insert it in between you will just log everything and pass the request or response to the next middleware so this is the this is the exciting part because it removes the need of frameworks the frameworks are so called trying to move to this middleware so that you can actually write simple classes that are framework independent future of PHP is collaborative is cooperating with one another not or I come up with my own level framework specific packages you come up with your own then framework specific modules then after that we cannot reuse easily so the future is is in middleware okay I can add to that with an example practical example that you can see for example you are the customer you request for a burger in McDonald's what happens is there will be a supervisor who is taking the order he doesn't do anything he just passes the request to the next guy the next guy just makes the patty ready and then give it to the next guy then that person will put it along with the bun and another person puts the mayonnaise kind of all the things that are required for the burger there will be a number of people who they will be just taking care of one particular thing and then passing it to the next guy when it goes to the last guy it will be returned back the first person who didn't do anything he just passed now he has to do a quality inspection is it everything is there is it up to the standard then he passes back to the customer so every one in the middleware has a role to play it can decide whether to play the role before or after like the supervisor he will play the role after I will just pass on then it comes back that is the time I want to take action for others who just work they will take action and then pass to the next guy so if the instruction has come from the customer say I want more latios I don't want mayonnaise kind of all those things it will be just passed on only the person who has to act on those instructions will read it and do it so the mayonnaise guy will see this guy doesn't want mayonnaise so he will just simply pass on to the next guy that's how the middleware works and there is one by Zen Zen if you don't know is the company the main driving force behind PHP so they have come out with this microframe what is called Zen expressive is a PSR7 middleware in minutes users PSR7 users middleware routing dependency injection templating and error handling currently I use it as an API so for example let's say my API has 1000 endpoints so I have one class for each endpoint and when I first started using this what I did was I only work on endpoints to get a request and return response I did nothing of authentication I never check for logging I never check for hideous later on when I wanted to add authentication to check whether the client authenticated client what I did was I created another action I put on top of this 1000 endpoints I don't go to all the 1000 classes and then put authentication check say call this class authentication function no I just create a new authentication class on top of the middleware pipeline later on I needed to do logging so what I did was I created another middleware class logger action and I just insert in between and that's all I don't need to so called change 1000 files and have them tightly coupled so basically everything is taking a request and response you either return a response or you pass it to the next middleware so this is the future PHP we should not be saying from this framework camp and from this framework camp no we are all PHP developers we should be making life easy for everyone and then we should make jobs easier for everyone as well now in closing let's see that's my twitter handle some people have asked me about my logo before so I'll just explain it here I was a freelance developer before I'm not freelancing now but this is still my personal avatar so you won't find my face on my twitter you will find this logo so int zone int zone we are all programmers we like maths Euclidean geometry we have a point, line and shape and prime numbers two two shades gray and white three triangle five, the five points of the pentagon and seven colors of the rainbow now white prime numbers according to Euclidean and Euler there is an infinite number of prime numbers the same can be said of learning of which there is no end so basically this is my personal motto to always learn to keep on learning and hopefully always to improve myself even if I hit the H or let's say 80, 90 and I hope that with this workshop even if you forget everything forget my face, forget the video I call this face, never mind I hope this will spark your interest in PSR don't just use a framework for the use of, for the sake of using it try to understand it and understand the underlying foundation what is it that enable this framework to be possible for you to be able to build a web application for us, one big factor is PSR, there is about 20 PSRs out there if we go to the PHP FIG website this is just touching on the surface I have only touched 1, 2, 4 and 7 there are 3, the logger interface 6, cache interface that thing is in review and we have a lot of drafts up to 17 so all these are up and coming please do take note of that and there is one of the speakers who is the secretary of FIG he will be speaking tomorrow or Wednesday do catch it as well so with that, thank you very much thank you Ziyong one before we break