Object Oriented PHP #11 - Static Properties
Key Takeaways
Explains static properties and methods in object-oriented PHP for utility classes
Full Transcript
or rather than gang so in this video I'd like to talk about static properties and the methods so in PHP a static property or a static method on a class is one which can be accessed via the class directly and not via an instance of the class so so far when we've been accessing methods in a class we've been taking the instance for example you know some kind of variable that we store that instance in there accessing whatever the method is called for example message and then invoking that now a static property does not require us to access it via a specific instance but rather via the class directly now this could be the case for some kind of utility class like a math class which could contain several different static properties and methods like a PI constant or other constants or maybe even methods to calculate an area of the circle or a sphere volume or something like that so what we're going to do is start off with a fresh class right here called weather and what I'd like to do is a few examples of static properties and methods so right now inside this we have a public property temp conditions and that's just an array with three of these different properties inside cold mild and warm we have a public function called Celsius to Fahrenheit it takes in a Celsius variable and then we're going to convert that to Fahrenheit shortly and then finally we have a public function determine that should be temp conditioned we're taking a Fahrenheit value and then we're going to return one of these conditions dependence on this value so at the minute if we wanted to access one of these things right here or this then what we need to do is say something like okay well we'll do a weather instance for example and set it equal to new weather like so and then we could say okay now I want you to echo the weather oops we need a dollar sign there weather instance and then grab for example the temp conditions like so now it wouldn't echo this directly we'd probably say print R because this is ray like so so if I say this it should work let me go over here and over here refresh and we can see we get this but this is long-winded for what is essentially going to be a utility class we don't actually need the instance we're not passing any data inside this class and it's just going to be the same for any kind of instance that we create so it makes sense if we could just access it directly from the class itself rather don't create a new instance and we can do that so I'd like to do something like this I'd like to say weather and then just use this this of this directly on weather so how do we do that well first of all we need to declare these things as static properties and methods and we do that by saying static in front of whatever the variable name is or in front of the function so static right here and also right here and now we're saying that these are static properties our static methods and that means that we can now just access them directly on this class itself rather than create an instance first and the way we do that is by using double colon and then whatever static property or static method that we want to access so if I want to get the temp conditions I just say dollar temp conditions now notice when before we use the instance it was dollar instance and then arrow and then whatever variable or whatever method so for example temp conditions when we did that we didn't have the dollar sign influence of temp conditions but we had it in front of the instance now we don't have a dollar sign in front of the class we have it in front of whatever static property we're trying to grab okay does that make sense so anyway now I'm going to print this out so I'll say print underscore R and then surround this thing save it and I already flesh and we should get the same thing awesome it works and that's much easier now we no longer have to create an instance to access these static properties we just get them directly on the class itself okay so then that's this example let's try creating this function right here so it takes in a Celsius value and I want to return in Fahrenheit value now luckily another formula for that so I'm going to say return first of all then we take the Celsius value we times it by nine and then we divide it by five and then we add 32 to the answer and that gets us the Fahrenheit so I can now say down here okay well this time echo and again we want the whether class double call long because we're accessing a static method and the method is Celsius to Fahrenheit we're passing a value for example 20 so 20 Celsius and it's going to echo out now whatever the Fahrenheit is based on that Celsius so save it and refresh and we can see 68 awesome so again this is how we access a static method same way the class name double colon and then whatever the method is called so let's do the last one now over here and what I'd like to do in here is determine temp conditions so we passing a Fahrenheit and based on that Fahrenheit we return either one of these things called my old or war so first of all we'll say if the Fahrenheit value that we take in is less than 40 then we want to do one thing and right here we want to return the basically cold value right here but we'll grab that in a second but for now let me just move on to the next line and that is going to be an else if and inside here we'll say if F is less than 70 then this time we're going to return the mild one right here again we'll grab that in a second so let's just do a comment for now and then finally an else case we don't need another if if none of these paths then we'll return born because obviously it's going to be over 69 degrees Fahrenheit and that's quite warm so we write here return and then the warm for you so right here what I'd like to do is access this static property and grab either the zero index one index or the second index so how do we do that because this is a static property now so I can't just say return and then the temp conditions because remember when I say this this refers to this instance now we don't have an instance anymore we just have the weather class so how can I do this instead well instead we say self and that refers to the actual class itself not the instance the class itself and then double colon and then a dollar sign and the temp conditions so a bit like down here whether double colon dollar sign temp conditions here inside the class we refer to the class as self okay so we're grabbing this property and now we want to return the zero index of that property and I'm going to copy that and paste it down here because this time I want to return the first position and then down here I want to return second position okay so if I save this hopefully fingers crossed this should all work in fact we need to call that function first of all so echo whether double colon because it's a static method and then determine whether or rather temp condition okay and we need to pass a value in so we'll say 80 and then we'll save this and I'll refresh check it out and it's warm awesome so that works if we change it to 20 save then we should get cold awesome and if we change it to 50 then we should get my old save that and refresh and voila we get mild now one more thing I want to mention you can add other access modifiers in front of static properties and methods for example I could make this thing over here private instead of public so I could say private we could still use temp conditions inside the class itself but if we try to access it like this down here it wouldn't work so there we go my friends that's static properties and methods in a nutshell they are still defined inside a class but they're called and accessed via the class itself and not on an instance of that class
Original Description
Hey gang, in this Object Oriented HP tutorial we'll take a look at what static properties are and in what cases we'd use them.
----------------------------------------
🐱💻 🐱💻 Course Links:
Course files - https://github.com/iamshaunjp/object-oriented-php
🐱💻 🐱💻 Other Related Courses:
+ PHP Tutorial for Beginners - https://www.youtube.com/playlist?list=PL4cUxeGkcC9gksOX3Kd9KPo-O68ncT05o
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
The Only AI Tools a Small Business Actually Needs in 2026
Dev.to AI
I Built an AI Life Planner the Month I Graduated and Switched to Linux Halfway Through
Dev.to · Hilal
Your Second Brain Is a Graveyard. Your AI Has Amnesia. (Part 1)
Medium · AI
The Python Automation Business I Started With 200 Lines of Code Eventually Replaced My Freelance…
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI