1. 43
    Understanding scope in PHP classes
    4m 1s

Understanding scope in PHP classes

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Class properties and methods can have different visibility levels, which control how private or protected those properties are.

Instructor: [0:00] So far, we've just dealt with the public scope on both class methods and class properties, which is the default scope visibility in PHP. However, two other scope visibilities exist, private and protected.

[0:15] Let's say we wanted to protect the author name so it's value cannot be changed outside of the context of this PHP class. Currently, we can update this class property outside of this context.

[0:29] If we go back to functions.php and go back to where these author instances are created, after this author one assignment, let's go ahead and assign a new value to the name property using author one arrow name. We will set this equal to Steve Jobs.

[0:51] If we save and refresh the page, Steve Jobs has replaced me in these blog articles. I don't ever want this to happen because over time I may have a lot of code floating around.

[1:03] It may be impossible to really predict which PHP classes change or modify other PHP class properties. Because of this, we can add a restriction to any class property to make sure this can't be done. Let's go back to the author class. Let's change this class property to private.

[1:24] This completely changes the visibility of this class property, so it cannot be accessed outside of the scope of this class. If we go ahead and refresh our page, we will get an error that tells us that it cannot access private property author name. This is exactly what we want.

[1:45] Let's check our functions.php file. Of course, this name assignment is now coming up as a red squiggly saying the member has private visibility. Let's go ahead and remove this assignment of Steve Jobs.

[2:02] You'll notice that this updated scope visibility is also affecting the output of these class properties. This is because this class property is now completely private.

[2:13] Since we now have no way to retrieve this name property directly with the arrow syntax, we need to create a new class method with a public visibility, which then retrieves the value of this property. Let's go to our author class and we will create a new public function named getName.

[2:34] This will be a function that returns a string. Within the body of this function, we will just return a call to this name. This refers to the class property name that's created within the constructor.

[2:53] This function is really simple, it just returns that class property. This means that we can now access the name property from outside of this class, by calling this getName method.

[3:05] Let's go back to our functions.php file, and update these calls to the name property to the method of getName. We will do this with every reference within this file.

[3:21] When we save and refresh the page, we can now see that PHP is properly returning the name class property by calling this public method.

[3:31] Scope essentially locks down how class properties are accessed outside of the context of a class.

[3:38] There is another scope visibility called protected, that we won't get into right now. Know that it's a slightly more open scope and visibility than private, has a lot classes that extend other classes access these properties.

[3:55] Private locks it down, so only direct instances of a class can access the property.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today