PHP Custom Functions & Objects / Scope Resolution & Static Components
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this lesson we're going to talk about how to use static properties and methods inside of PHP objects and how to use the scope resolution operator. In the 0609.php file, we have our code left over from our previous lessons. Here we defined a simple class called FileMaker and then we extended that class with the my FileMaker and then we created some instances of objects based upon those classes. And then we outputted it to the browser. Let's see what the code looks like in the browser before we make changes. Here you can see that we're accessing the property of our class by using the public function called set version and get version. Now we're going to talk about creating static methods and static properties. First of all, remember that we don't have access to any of our properties or our methods unless we first create an instance of the object. Once we create that instance and we assign it to a variable, then we can use that variable name to refer to the method that we want to call or the property that we want to access. Inside of your code of your class definition, you can refer to it's internal properties by using the this keyword variable and then mentioning the property name. This points to this instance of the object. So this version is different for my software than it is for another software if the another software was a different instance of the FileMaker object. Now, we want to talk about working with methods and properties before we've created any instance of the class, so there is no object we're working with. First, let's define these methods and properties. These are called static properties and static methods and to create them, you use the same access keywords, such as private, and then you use the static keyword. We're going to create a private static variable called type, which will tell us the type of our software, whether it's FileMaker Pro, Pro Advanced or Server or Server Advanced. For the class FileMaker, we'll say that it's type is Server. Here we have a private static property. This property can be accessed without ever creating an instance of the actual object. Now, because it's private, we need to have a method which can be used to actually access that property. And that method needs to be public. So we'll say public and then we'll say static again because we want to use this method before we create an instance of the object and then we'll say function. The function's going to be called type and it's going to return the variable type or static variable. Now, since we're going to be using this method before we've created an instance of the object, we can't use the key variable word this. Remember, that refers to this instance of the object and we don't have an instance of the object. So in order to use the actual class variable type, you need to use the word self. Self refers to the class itself and then you use two colons. Two colons means this class or the scope of this class. It's called the scope resolution operator. Self colon colon and the variable type. Now we can call this method without ever creating an instance of the object and it will refer to the class itself by grabbing the static variable type and returning it the calling code. Let's close our function now and use this in our code below. First we'll echo out some break tags and then we're going to say echo and we want to call this method. Now before when we called the method, we would have to specify the object first by it's variable and then use the dash and the greater than sign to specify the method. But we're not creating an instance of this object. We're going to call the method of the class itself. To refer to the class itself, you simply use the name of the class. Our class is called FileMaker so we'll say FileMaker and then we use the scope resolution operator and then the name of the function or the method that we want to call. Now we're going to be talking directly to the class and performing the static method of that class without ever creating an instance of the object. Then we'll echo out a couple more break tags and refresh this in the browser. Here you can see that we've returned or outputted the static property type which is Server. And we did this by calling the static function type without ever creating an instance of the object. This is how you define and use static methods and static properties by use of the scope resolution operator. And this concludes our lesson.
Tutorial Information
| Course: | FileMaker 9 & PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 33786 |
| ISBN: | 1-933736-99-2 |
| Release Date: | 2007-08-22 |
| Duration: | 9.5 hrs / 107 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 