Basic Class With Variables

Ultimate PHP and MySQL Web Development Object Oriented Programming
4 minutes
Share the link to this page
Copied
  Completed
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state and implementations of behaviour.

Transcript

Hello, in this video we are going to take a look at implementing a basic class with some variables. So to implement a class, we'll simply just put the keyword class and the class name. Cool. Then some curly braces inside here is where you put all your variables and functions. In a separate video, I'm going to cover a function. So this one is just purely for variable.

So we'll create a variable for public. And I'll explain know what public is in a separate video because it comes into you know, in relation with private and protected keywords, which is for scope, but you know, I'll cover that in a separate video, the public then the variable name, so let's call it dollar speed, you know, vehicle speed equals 10, semi colon. And if I just try and print out speed, reload, we get an error on the phone variable on line eight because this is only defined within this class. class. So to actually access anything that is inside the class, we need to essentially create a object of it, create an object of it, you if object is like a variable is just a variable of a particular class, so it's called an object. So first of all, you name it like any other variable, call it OBJ equals, and then you put new, which just reserved some memory for this particular object, vehicle, semi colon.

And now if I want to print out the speed of an echo, dollar OBJ, then this up within the pointer operator. There we go a printout 10. So what's the benefit of doing this? And let's say if I create two objects, object and object to and then what I do is, I'm going to print out both of this And you know, I'm just going to put a quick new line tag just just so it's easier for formatting and at the start, I'm also going to put vehicle one speed. And I'm teaching this to be. So if I run that we get Vic wants to be 10 with to speed 10.

But watch what happens if I do this. If I say dollar OBJ that's the safe one, modify the first one. I'll do B now equals 89. And now, if I print out both of these as you can see, vehicle one vehicle two have the same speed because that is the default speed setting here. Wi Fi modify your property One of my vehicle, it does not affect the property of ANOVA. So that's great if you're creating a game that has, say 1000 enemies, and the role of essentially the same type of enemy, but they all have their own unique position, their own unique health, depending on how much damage they've taken, they could potentially pick up a weapon.

So it can be some sort of AI, and he has his own properties. But at its core, it they're essentially the same. This way, you could create classes, essentially like a template. And then you create multiple objects of it without having to duplicate code again and again. And if you updated, let's say, the vehicle class with no more than features, it will propagate to all of your objects that have been created of this class. So this is basically for a basic class with variables over you know, several videos on the cover or the little nuances of classes.

And as you can imagine, And you can add more variables. You know, he's pretty easy as an extra task that's allowed me to do is add a number variable. So that's it any questions, feel free to pop me a message. And as usual, I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.