Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Up to this point, the only way we've used classes is by instantiating, which can get a bit unwieldy. In this chapter, I'm going to introduce the concept of the static keyword, which gets around this limitation. I'll then demo how to access class contents without instantiation, set values that never change, and even how to create a method that doesn't require instantiation. Finally, we'll put it all together, and create a custom database class. But before we do that, let's start with the basics, avoiding instantiating. Sometimes, it's useful to access a property or method of a class without the overhead of actually instantiating an object.
Depending on the class, that instantiation might include connections to a database or third-party service, which can drag down performance when all you needed to do was get a default value. Also, before the release of PHP 5.3, the only way a developer could prevent a function or variable from having global scope was to declare it in a class. Finally, there are circumstances when you want to create only one instance of a class, and share it across the application, so it becomes important to be able to limit instance creation. To do this, we'll use the static keyword.
This makes the property or method accessible without needing instantiation. Static properties are unique in that their values are stored at a class level, not at an object's level. This can be useful to keep track of the number of instances, which I'll demonstrate in a bit. In the Address class, I want to keep track of the different types of addresses. For this demo, we'll keep it simple with three address types, each with a unique ID. Let's use 1 for residence, 2 for a business, and 3 for a park.
We're going to assign labels to these values, so we can display them to users, and even use the values for validation. As the address type is specific to addresses, we'll associate it with a class. By using the static keyword, we can expose these address types to other parts of the script without instantiation. To declare a property static, we'll use the same notation as we did with regular properties, including scope. The only difference is we'll add the keyword "static" somewhere before the property name. The order is actually flexible, but it's best practice to stay consistent. I prefer to start with static.
The declaration will end up looking something like static public $variable = . . . and so forth. Let's open the Address class. Then, I'm going to add the following to the top. Address types. static public $valid_ address_types = array, 1 is a residence, 2 is a business, and 3 is a park. Save, then open the demo file.
At the end, let's attempt to display these address types. echo
Address types . . . echo
var_export, ($address->valid_address_types, TRUE),
Then close pre and tt. Save then reload the browser.
Instead of the expected result, there is an error message citing an undefined
property. This is due to the static keyword.
In the next video, I will describe how to access a static property using a scope resolution operator.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98509 Viewers
61 Video lessons · 85804 Viewers
71 Video lessons · 69708 Viewers
56 Video lessons · 102013 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.