- Overview
- Transcript
5.3 Static Properties And Methods
Using static properties and methods can help you write less verbose code. But bear in mind, statics do not behave like true objects.
1.Welcome1 lesson, 01:07
1.1Welcome01:07
2.The Absolute Basics of OOP5 lessons, 22:53
2.1What Is OOP?04:40
2.2Classes vs Objects05:42
2.3Class Constants and Internal Reference04:19
2.4Public vs Private Scope06:05
2.5Copying vs Cloning Objects02:07
3.Digging a Little Deeper Into OOP5 lessons, 43:52
3.1The Single Responsibility Principle15:18
3.2Magic Methods11:16
3.3Autoloading Through SPL03:37
3.4Working With Namespaces05:14
3.5Autoloading With PSR-008:27
4.OOP inheritance2 lessons, 12:02
4.1Class Inheritance And Protected Scope09:22
4.2Overriding Parent Methods02:40
5.OOP Abstractions5 lessons, 1:00:05
5.1Abstract Classes05:59
5.2Interfaces14:53
5.3Static Properties And Methods06:49
5.4Traits14:44
5.5Dependency Injection17:40
5.3 Static Properties And Methods
Hello, and welcome back to PHP OOP Fundamentals. In this video, we'll take a look at static methods and properties. Now statics are often frowned upon. And this is because they behave basically the same as global functions and properties, although they exist in a different namespace. I'll give you an example. Let's create a function to provide for quick debug dumps. So we'll just do function, and let's call it dump and pass a variable as a parameter. And then inside of the body we'll just do var_dump, and then we'll just pass in the variable as the only parameter we have. Now, maybe to spice it up we could precede it with maybe a pretag. Okay, and let's just copy that and make sure we append it with a pre-closing tag as well. Now all that we need is something to dump. We'll just create a post variable. And that will be equal to a new standard-class object and maybe we'll just set a title for the object. I make that = 'My title' okay? Now, let's make use of our dump function and just pass in post there. Make sure we close that out with a ; and let's view the results in the browser. Okay, well that seems to work just nicely. Now, there is a problem with this function. At the moment it just lives in our index or PHP file. But if it were defined elsewhere, we would have a clue just where exactly it was defined. It just sort of magically exists. So why don't we make that a little more obvious. Let's wrap that function inside of a class. Just down below, I'll create a little class called Debug and now all I need to do is take this, cut it, paste it back in. And make ure it behaves as a public function. Now of course, this dump core here won't work any more. Instead, we'll need to instantiate a new object from the class that we defined down here. So I'll just create a variable, and let's call that $debug. And I'll make that equal to a new instance of the debug class, okay? And now I can do debug done, like so. You see? When I go back to the browser and reload, nothing has changed, it still works. Okay? Now that's a little better, especially if I stored this somewhere within my Acme Name Space, I would know exactly where it lives. Only instantiating a new Debug object every time we want to something as simple as dumping something to the screen, it can get a bit cumbersome. So what if we wouldn't have to instantiate this class first? Well, that's when stag methods come into view. The clear class properties, or methods as static makes them accessible without needing instantiation of the class. Now remember this is what often is looked upon to be bad practice, but since since statics are in the OOP manual it seems only natural to give you at least a few examples. Now, all I need to do to create a static method inside of a normal class is prepended with a keyword static. From now on I don't have to instantiate that class anymore to be able to access it. Instead I can just do Debug, followed by ::, which is the operator with which you call static methods, and then just followed by the name of the actual method. Okay. Let's reload and that works like a charm and it saved us some typing as well. Now, you might wonder could we use static properties as well. Well, yes we can. You just use the static key word again, and create a property like you normally would. Let's create a wrapper start and a wrapper end property, like so. Now wrapper start will be equal to a pre-start tag. River N will be equal to abriente how do we access these properties here? Well I can't use this, because we haven't got an instance of this class. Instead I'll have to use self followed by double colon, followed by the property name, preceding with a dollar sign. I'll just fix this typo here. Here we could do something like self :: wrapperEnd and that will work as well. We'll just do one more and we'll just call that foo and it will be equal to the value of Bar, like so. So now let's see if we can access this property outside that class as well. We'll just do debug::$foo;. And you know what, let's just echo that to the screen. Now of this works, we should see Bar echo to the screen. Let's fire up our brower again, and there you have it, Bar. So you see, static methods and properties essentially behave as a sort of name spaced functions and variables. You don't have to first instantiate this class to have access to its properties or methods. Well, as a matter of fact, you can instantiate that class. Only it won't behave like you would expect an object to behave. And this is why statics are often frowned upon, and that's because they don't behave like true objects. First, I'll just comment this out. And then I'll create a new variable called dumper. And that will be equal to a new instance of debug. Very simple. Now I'll just set this property here to a different value. So, I'll do something like $dumper::$wrapperStart ='New value'. Okay and then I'll just say echo $dumper::$wrapperStart, and that should dump the new value to the screen. Which it does, so that's okay. Now let's create another object which we'll call $anotherDumper. And that will be an entirely different instance of Debug. So that will be new Debug like so. And then we'll echo something to the screen. We'll do $anotherDumper, and let's just say that same variable, so wrapper start like so. Now if these were truly different objects, then this would echo New value, and this would echo pre, like the default value which is defined within the Debug class. Instead, what you'll see is that the second time around, the value of the property wrapasod is still set to New value. In other words, you'd think you'd be looking at two different values for two different instances. While in fact, you're looking one and the same value. So yes, static classes can save you a lot of typing, which can be very nice. But they don't behave like true objects. So be sure to bear that in mind. Happy coding, I'll see you in the next video.







