Object Oriented PHP #11 - Static Properties

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·6y ago

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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
The Only AI Tools a Small Business Actually Needs in 2026
Discover the essential AI tools for small businesses in 2026 to boost efficiency and customer engagement
Dev.to AI
📰
I Built an AI Life Planner the Month I Graduated and Switched to Linux Halfway Through
Learn how to build a personal AI life planner by following the author's journey, from initial development to switching to Linux halfway through, and apply these skills to your own projects
Dev.to · Hilal
📰
Your Second Brain Is a Graveyard. Your AI Has Amnesia. (Part 1)
Learn why your second brain and AI tools are failing you and how to address these issues
Medium · AI
📰
The Python Automation Business I Started With 200 Lines of Code Eventually Replaced My Freelance…
Learn how a simple 200-line Python script can turn into a full-fledged automation business, replacing freelance work
Medium · Programming
Up next
How AI Is Transforming Analytics in Tableau Cloud & Server
Salesforce Product Center
Watch →