 Hey everybody, this is Brian, and welcome to the 34th LAMP tutorial. Today we're going to be discussing scope. What is scope? Well, let's make a class here, and we'll call it person, and we're going to make a variable called name. Actually, we want to give it an access modifier public, and let's make a function, show name. And this is just going to print the name out on the screen, and this is actually a common mistake I see a lot of people make. Can you spot the mistake while I'm creating the variable? And we'll say me, oops, and let's call that, and let's run this and see what happens here. Absolutely nothing. Why? It's printing nothing at all. Well, let's actually initialize this, and now you're thinking, well, you didn't initialize it. Well, let's initialize it with something different. Say Heather. Notice how it prints Heather, but up here it says Brian. This is called variable scope. Now what we're saying is this variable has a global scope to the class, and what does that mean? When you create a variable, it's accessible from its creation point where we are and all subcode blocks. For example, in this, notice how we have the name, but it's different. We're actually creating a new variable here. When it comes to PHP, and I should say most programming languages, you need to use what's called the this pointer. It's a pointer to the current object. Let's actually create two of these so you can really see what's going on here. I want you to really understand the difference between this name and just name. Notice how once is Brian, once is Heather. Well, here's our name variable with the global scope, and here's our name variable with the scope local to this function. What's going on is we're creating a whole new variable. It may have the same label, but it has a totally different scope. Now that's bad practice, and you shouldn't do that, because as you can see, it's very, very confusing. You don't want to do that in the real world. I'm doing it for illustrative purposes to show you what not to do and what to watch out for. You access global variables through this keyword. Now, do you think name is available outside of this function? No. Why? Because it's created at this level, at this code block. What if we say something like if true echo name? What's going to happen here? Well, because we create the variable here, it's accessible to all subcode blocks. So that's how ifs, then, do, while, loops, all other little code blocks work. You can create a variable, and it's accessible inside of those code blocks as well. It's not accessible outside of this function, say new. The reason is because it goes outside of its scope, its code block. However, the public one is available. So in this case, it would be yes. Pretty simple, pretty easy concept, but hard in practice. In the real world, never duplicate variable names, never, ever, never ever do that. Why? Because it gets confusing, and it just really, really bad programming form. Now, I want to discuss also very quickly visibility. You see this access modifier called public. Let's actually create a few variables here. Protected, and we'll say age, I'm 38, and private, and we'll just say ID. Notice how we have three different access modifiers, or three different levels of visibility. Public, protected, and private. Now, what do these mean? Public means it's accessible to the whole world. Notice how we can actually access this from outside of the class, because it has a public access modifier, name, name. But we don't see age or ID. Why is that? Well, protected is only to the current class and all classes that inherit it. So if we say person, oops, actually, I don't know how to myself class, person extends. I can't use person, can I? Let's say employee extends person. And let's just grab a function here. Call this our test function. Oops, got a little ahead of myself there. Let's just say this. Notice how now, because we're in an extended class, we have access to age and name, but not ID, because ID is private. Private means it's only available to that current object, that current class. So you could use it in this function show name, but you can't use it down here in other classes or outside of the class. So let's actually create a employee. Ah, can't see my mouse. Let's call it myimp, new employee. Notice how once again, visibility comes into play. We can only see name, because it's the only public one. Now, you can also do that with, you guessed it, functions. And then we'll say internal only, just as a name. Function does absolutely nothing. Oops, should probably put the word function in there, so that knows what I'm talking about here. That function does absolutely nothing, but we can see it within extended classes. Internal only, see right here, because it's protected. But if we try to access it outside, notice how suddenly it's not there. And yes, you can make functions private, so they're only accessible through the current class. That's all for this tutorial. I hope you found this educational and entertaining, and thank you for watching.