so in this episode we're gonna take a look at our first project we're gonna do together well it's not really a big project but it's more or less of a small exercise no-1 to actually use what we've learned so far in object-oriented PHP and try it out inside something we can actually use for something I am wearing my leather jacket it is slightly cold inside my new office this is a new office by the way in case you didn't know right now I'm using this opportunity to add this this excuse to wear something cool now
I do apologize if you can hear some squeaking because leather jackets apparently are a little bit squeaking when I move around so I hope it doesn't get captured by the microphone but what we're going to be doing here is we're going to be doing a calculator which is sort of a tradition to do on my channel so I thought we would do that as well when it comes to object oriented PHP so what I have so far just to sort of tell you where I am at the moment so you can sort of catch up
to it before we start doing the actual PHP part because what I've done here is just a little bit of pre-work which is basic HTML so it's not really something that you haven't learned before watching this course here so what I have so far is I do actually have my index page I still have my strict types set to one up here at the top to make sure that if we declare or do type declarations inside our PHP code that we can actually use it properly I do also have this include to include my autoload
function which out of those classes if I were to reference to a class inside our code here and at the bottom I do also have a basic HTML form so inside this form you can see that I have first of all a small title that says my own calculator we could also put it on top of the form doesn't really matter and I do also have an input for the first number we're going to do for the calculator I just simply set the type to number set a name to num 1 which is what we're
going to reference to in the next document that we need to run in order to actually perform the PHP code and then I just have a placeholder that says first number below here I have a selector so we can select to add subtract divide multiply again you can add to this if you want to and create something quite a bit more complicated than what I have here this is just a basic example to give you an idea about how we implement classes in regular PHP stuff or just regular you know functions inside a website and
then underneath here I have the second number which is identical to the first one I just changed it to num2 instead of num1 and then said second number instead and then we have the button at the bottom which is pretty much the last thing that we need to have and it's just a regular submit button it just has a title or a text inside of it as calculate so it's very basic I can't show how it looks like inside the browser right now because we do have an error message that we need to take care
of so what we need to do here because there's a little bit more work to do is I'm going to go inside my root folder of this project here at the same place where have my index file and inside my root folder I do have two folders I have a includes folder that has two documents in it and I have a classes folder now inside my classes folder what I want you to go ahead and do is create a new file called calc the class the PHP this is going to be the one that has
our calc or calculator class inside of it as you can see right now - empty I can actually go and zoom in for you so you can actually see what's going on here and in sap my includes folder I have a outer load document which is the one that we learned about previously in the previous episode I do want to point something out here because as you can see this is actually what we learned from the previous episode but in some cases inside a website like the one we have here if I were to use
this function here it works perfectly if I'm inside the main directory of directory of my root folder meaning from my index page but if I were to be inside let's say it includes folder and I have a document loaded inside the browser from the includes folder and I want to load the same outside load function I need to change the path inside this outer load function so what I did is a little bit further down is I created as second version which we're going to replace with the the one we learned from a previous episode
so what I'm going to do here is I'm just going to go ahead and delete all the way up to the top just so you can see the the two of them at the same time and all I want you to notice here is that I included well actually this one was not part of the one you had as well so there we go now it looks like it's supposed to what I included is a URL inside a variable so the URL file current web site and if we do have includes as a string inside
the URL it means to go right now inside the includes folder which means that the path has to change so all I did was I created an if-else statement that says okay if we're inside the includes folder then we're going to just create a dot dot forward slash inside the path in front of the path to tell the path to go back one directory and then inside our classes folder so that's all I did here I just added this small if-else statement and everything else should basically be the same so replace this above here with
let's actually go ahead and make sure we don't delete the PHP tags go ahead and replace that with what I have here so make sure you don't get some kind of error message so after doing this you can actually see that we don't have an error message setting more so in here you can actually see the calculator we build inside the index page so this is progress we have a calculator now so what we can do is I can go ahead and type in a number we can also do what we can do here you
know go up and down in numbers because I set the type to a number type we can choose a second number and then we can choose whether to add subtract divide or multiply and then we can then calculate then it's going to take us to another page where the calculation is going to happen now right now nothing's happening because if I were to go back inside our project you can see that inside my includes folder I have a calc that Inc the PHP not a dot class the PHP is not the class page if I
were to load up the class page this is the class page no the other one is called calc dot Inc the PHP which means that it's just an include file that doesn't contain classes but it's an include file that we load in the website they we don't actually see inside the website that's basically why I call it dot Inc something I think I've explained this in the past before so this shouldn't really be new information I think but inside this file you can see that nothing except for an opening and a closing tag when it
comes to PHP code and what I want to do here is I want to actually grab the data from the form because this is the class that we or the documents that we load up once we click the submit button so I want to grab the data meaning the first number the operator type and the second number and then I want to run them using some of the class methods that we're going to create inside our calc class so let's go ahead and start by creating our class first so in this example here since we're
creating a calculator it would make sense that we create the properties first in this example is going to be three properties so we're going to say we have a public if I can actually see something cuz my microphone is in the way we have a public operator to start with because that is going to be the first the first one we have here then I'm going to create a public num1 so public one it's kind of good I'm typing slow because of the microphone because then you can actually keep up with the lesson here I'm
also gonna go and create a pup League num let's actually do this where is it there we go num2 need to find a solution for this in the future so once we have the properties here we need to create the methods now these properties right now don't have a value we haven't said it yet so what we need to do is once we load this class and actually instantiate the class and create an object is we need to fill in these properties once that happens and we do that using a constructor so we're going to
create a public and I'm just gonna say public function and we're going to call this one underscore on the straw construct the event actually typed this correctly there we go missing quite a bit up because I can't see the keyboard inside this constructor we're just gonna go out and fill in these properties so we're gonna start by just adding in what kind of data we want to have inside the parentheses of the constructor so I'm going to start by saying I want to have a string datatype and this string data type is going to be
the one called upper rater just so we have the same name as above like do keep in mind that what I have here is not this thing here the information we have inside the construct parenthesis is actually the data that gets passed from the user inside the form not the properties up here we could actually do something slightly different just so you don't get confused let's go ahead and call this one this is going to call this something like 1 because I want to show you exactly what is going on here because some people might
have missed this concept so I'm just going to say we want to grab a string which is called in this case year 1 then I want to grab a integer datatype this one is going to be called 2 and then I want to grab another int which is going to be the second number and I'm just gonna go and call this 1 3 is that how you spell 3 that is how you spell 3 it's either with without the 8 but in this case it's with the age so now we have these three different parameters
that gets passed into this method that we're going to assign to these properties up here so the first one I'm going to say that we have this then I'm going to point to the property that is upstairs so I'm going to say operate or and in this case here I am actually referring to this one up here I could actually just copy-paste since I sort of messed it up down there make sure we don't reference to variable but just the property operator and then I'm going to set it equal to the data we pass in
here which is going to be the string let me pass in so I'm just gonna say it was going to be equal to 1 now does it start to make sense why we don't necessarily have to call these the same as here like in practice we do actually call it the same so I would actually be naming it like this but it doesn't refer to this up here it is just a thing that we you we do as PHP programmers to make sure that makes sense further down here so when we pass it in it
makes sense because it looks like they go together but you don't have to that's what I'm trying to say here the second one we're going to create is going to be the number one that we have up here so I'm just going to say we have number one without the variable and that is going to be two T wo then we're going to have num2 without the variable that is going to be three like so so what we basically did here is we said okay once we create this object based off the class here at
the same moment we're going to take three different parameters and we're going to assign them to these properties inside the class that's all we're doing here we don't have to do anything else inside the constructor here what we're then gonna do is we're gonna go ahead and actually create a method that actually calculates this calculator for us so what I can do is then create another method so I'm gonna say function let's actually make it public first public function and I'm just gonna go ahead and count it a call this one calculate or parentheses curly
brackets and do note that one of the differences between like we could pass in three different parameters inside the calculator method here I could have passed in the operator the number one and number two inside the calculator method and then calculate it that way but because I want to use operators in this class here and just assign it we're going to do that way so we're gonna use the constructor to create to pass in the parameters first and then we don't need to do down here the method later on kind of a neat thing so
inside the calculator I'm going to just go ahead and create a switch just regular PHP switch not the Nintendo kind ha okay so inside the switz here we need to have some kind of data that it's going to say well depending on this piece of data we're gonna do one or the other thing so in this case here I want to say well depending on the operator that was chosen and do keep in mind when we create the object the operator up here is going to be set like it will be created meaning that I
can just go and refer to this operator inside this class here a two thing I spelled this wrong operator that's with an O isn't it let's go ahead and change that there we go I think that is correct isn't it I have notes in this ideas I know for a fact this is correct I'm just looking notes and thinking huh that doesn't look the same so well this is how we do it so we reference to this operator inside or the operator property inside this class here and depending on the value of it if it
does say that the value is add which we're gonna add later once we create the object then it has to do something specific down here so what I'm going to do is I'm going to create a variable called results I'm going to set it equal to then I'm gonna again point to num1 which is the operator we have inside this class here and then I'm gonna say plus num2 which is a property inside this class here inside the object so what I can then do is I can go ahead and say I want to return
and then I want to return result and that's basically it now we can actually add something inside our calculator at least when it comes to the class part and it will be working so we also need to create one for subtraction we need to create one for division and one for multiplication so if I go down here and just go ahead and paste it in because there's no need to not just paste it in it's just gonna move it back so it matches up perfectly there you go there might be some bumping inside the microphone
because my keyboard is also kind of loose on the table there I'm gonna change the first one oh we do actually need to move back one more there we go I do need to change the second one so it is right now sub four subtracts I want to change the second one too dev for divine and then more for multiplied and then we just need to change the you know the operators in here so it's going to be - it is going to be divided and it's going to be multiplication that is not multiplication this
is multiplication I did pass basic math once so I should know how to multiply then if you go down inside the default outcome of the switch in case some kind of error happens then we want to just simply I don't know echo out a message to something so we're just going to echo out era like so just to notify that there was an error message inside what we did here once we've done this we do actually have everything we need to have inside our class so now we're actually done with our calc class so if
I were to go back inside or calc Inc the PHP file the one that actually gets run after we submit the form then we can actually go ahead and do very little in here actually because remember the class does most of the work we just need to reference to okay so now we have all the work done over here now what do I want to just like use from the class in order to do what I need to do here so doing it with classes is a very cool process because we can just keep reusing
code again and again and again and that's one of the big perks about using classes instead of procedural PHP because then we'd have to rewrite the code again and again and again so if I were to go in here the first thing you need to do is you need to again declare district types because that's the thing you need to do I do also need to include our outer load function that is inside our out load that Inc the PHP file so what I'm going to do is I'm going to change the path because it
we don't need to dig in sudden includes folder anymore because we are right now inside the includes folder so I just need to delete the first part of the path here let's just go and move it out so it looks nice and shift everything down just to create some spacing so what I need to do now is I basically just need to grab the data from inside the index space when we let me actually submit the form so we're going to say well we have a barrier just gonna call this one upper for operator and
I'm just gonna go and set this one equal to dollar sign underscore post and we're just gonna go and grab the operator now we didn't call this one offer at least I did inside the inside the index page just cuz I got the question from a previous episode so let me just tell you why I reference to upper if I go back inside my index page we'll have my form and go down to one of the inputs let's take the selector here you can see the name is set to upper that means when I submit
this form and again this should be basic HTML you should notice by now but I'm just gonna say it again then once we need to grab the data from the form here we need to reference to upper to get the data they selected here then with number one because name is that equal to num 1 we need to reference to num 1 and then the second one down here is num - so that's why we do that just in case needed that refreshed which again is okay it's okay to get stuff refreshed but it is
one of those things that you should be very familiar with right now so num 1 and then I'm just gonna go ahead and copy paste not one just gonna copy paste this part here there we go and change it to two and two so now we have all the data we need from the previous form and now we can actually start running the calculation which means all I need to do is I need to actually create an object off the class called calc so what I can do is I create just a variable called calc
this is going to be the object and I'm going to set it equal to new calc parentheses semicolon and do remember because we have a constructor inside here this constructor right here we do need to pass in three different parameters so if I were to go back again you need to include those three which is going to be upper which is going to be num1 and it's going to be num2 now there is going to be a small error here though because when we pass in the data it is going to pass num1 and num2
as strings that we need to have integers because remember once we went inside our constructor we did actually specifically ask for integers so because of the type declaration we do actually need to do something called casting which means that once we do do something these numbers I'm just gonna cast this to an int to convert it to an integer this is something we can easily do if you can just like the regular do it so I'm just gonna cast it to integers this is also something as basic PHP but I don't think that I taught
it in my PHP course I am remaking the PHP cause right now that this will be something in that course but we're just gonna cast it to make sure that it's an integer type ones who do actually use it so now that we did this all we need to do is run the method that actually calculates the whole thing so going down to the next line I'm going to run a try-catch block of code and instead of an exception we're actually gonna try and catch a type error so we're going to say type if I
can actually spell that type error and the error message will then be variable e if we do actually have an error message so what I want to do here is if I get an error on I just simply want to echo I'm just gonna get an echo a string called error on print exclamation mark colon space then I'm just gonna go ahead and attach variable E which is going to be the error message and then say I want to get the actual message I don't think we've talked about this in the PHP course either but
this is one way to just simply grab an error message when you do like a a try catch block so what we need to actually do if we succeed it is going to run the block of code inside to try part of the try catch block so what we just basically need to do is we need to actually run the actual method so I'm going to echo and then I'm going to say we have variable and I want to point to a method called Cal cool Latian that did not get spelled correctly I actually did
we call it calculation or calculate called a calculator it's mostly easier just to copy paste there we go and like so so now we should actually have a working calculator so if I were to go ahead and test this out which would probably be a good idea to do beforehand if I'm gonna go and choose number two say I want to add on to a choose number three calculate you can see we get five if i zoom in if I go back again and you can actually everything shifted around because I zoomed in but that's
okay I can choose five and maybe two three let's change it to two and then say I want to multiply calculate then you see we get ten so this is how we actually use classes and methods and properties in an actual example one of the things I know people get confused about is they think they have to use the class and the methods and stuff inside the actual index page where the form is but you're actually supposed to create a separate file for example an include file like this one I created here and then run
all the calculations and methods in here once you grab the data from the form so that's how you need to go about doing that because that is a question I get quite often when it comes to this like how do you use classes together with forms inside let's say a contact page or something and you do actually need to do that instead of separate document together so I hope this was kind of useful we will continue with these lessons in probably tomorrow the day after tomorrow or something because I am in a new office so
there should be more frequent uploads but I hope you enjoyed this episode and I will see you in the next one [Music] [Music] [Music]