Object Oriented PHP #2 - Intro to Classes
Key Takeaways
Introduces object-oriented programming concepts in PHP by explaining classes and objects
Full Transcript
okay they're my friends so the first piece to the puzzle when you started out in object-oriented programming is to understand what classes are now classes are like blueprints for objects in programming so much like we have a blueprint for real-life objects in our lives for example like a train or something which describes how to build a train and how that train should behave we also have blueprints or classes in programming which describe how objects in programming should look and behave so by that I mean what properties it should have and also what methods it should have and methods is just another name for a function a method is a function associated with a class okay so when I say methods it's kind of interchangeable with the word function as well so a class describes what properties an object should have and also what methods an object should have so just back to our train analogy the properties could be something like the length of the train or the color of the train or maybe the top speed of the train they would be the properties of the Train the things that describe it and the methods or the functions of that train would be to accelerate so we can move or to break they're the things it can do right so imagine we have this blueprint in real life to create trains now every train would have these properties and every train would have these methods now the values of the properties mind differ for every train that we make for example the color or the length of each one might be different every time we make one using this blueprint for a train but they would all still have these properties and they would all still have this basic functionality accelerating and braking so in programming we're not going to have a class which describes a train well we might do but we might have something like a user instead and the properties of that user could be a username and an email so all users would have those two properties and the methods could be something like add friend to add a new friend or post status like on Facebook you post the status so they could be the methods and these are the properties and together these describe the user how they should look and behave now again every user that we created using this kind of blueprint or class they could have different values for the username and the email but they would all have those properties so say for example we'd have three users these would be the values for those two properties but they would all have those properties they all have a user name and they all have an email and they would all have access to the same methods as well so all of these objects that we create could all do the same things add a new friend and post a status so in programming in the future if we ever wanted to create say a new user object what we do is create a user class defining the properties that that user should have or that user object should have rather and define the methods that that user objects should have and then we'd say to PHP okay make me a new user object based on this user class and that would create that new user object for us with these properties and with these methods so now we know the basics let's have a look at how we create a class in PHP and then ask it to create an object based on that class as well all right then so back inside index.php let's have a bash at creating our own user class so the first thing I'm going to do is get all right of all that bumf we don't need that trash anymore and then we're going to create a class and the first thing we do in PHP to create a class is use the class keyword then we give this class a name now I'm going to call this use it now notice two things here first of all I've capitalized this and that is convention in PHP whenever you name a class give it a capital letter and also this is not plural it's singular and even though this will be used to create multiple different user objects we don't call it users we just make it singular and call it user okay so now we have curly braces and inside the curly braces this is where all of the juicy stuff describing our user object is going to go and by that I mean this is where all of our properties and methods are defined okay now we're not going to do that just yet instead what we're going to do is create a new user object based on this rather boring class hit them in it but let's create one so we create a new object based on a class by saying new and then whatever the class is called so in this case user and then we invoke that much like reward a function so parentheses at the end and this goes out it looks at the user class and it makes a new object based on this user class now at the minute that object is going to be completely blank and empty with no properties and no methods because we've not defined any right here so at the minute it's not really a user it's just an object but it's made that object based on this user class all right now if we wanted to what we could do now is store that user inside a variable in case we wanted to use it later on in the code so I could say something like user 1 is equal to a new user so it creates a new object based on this class right here and it stores it inside this variable now just one thing before we move on this action of creating a new user based on this user class this is known as instantiating a class because we're creating a new instance of this user class right here and we're storing it inside this variable because an instance of something is at the end of the day just a single occurrence of something so I will use a one variable right here and that is a single occurrence or an instance of the user class so that's just a bit of terminology this is known as creating a new instance of this class or instantiating that class all right now if we ever wanted to create a new object based on this class in the future we could do that we could just say something like user two is equal to a new user right now and that would create a new object based on this class so how do we actually know that these are based on these classes where we can use a function in PHP and that function is called get underscore class and if we pass in an instance a variable which stores an instance of that object inside this function so I could say user 1 right here it's going to tell me what class this object is based on now I'm going to echo this out so we can see that it works so I'll say echo now we'll do a string before it and concatenate it with a dot and this string just says the class ears and then space and then it's going to concatenate whatever the results of this function is which should be user because it's going to look at this instance and say hey yep this is created using the user class so if I save this now and on refresh over here then we can see the class is user and if I change this to user two then it should be exactly the same so let me refresh again and we can see the class is still user so there we have it we have now two pretty boring user objects that definitely user objects but that doesn't really mean anything at the minute because they have no properties defined up here on methods they don't do anything so in the next video we'll look at even different properties to this class so that at least it can do something when we create that object
Original Description
Hey gang, in this OOP PHP tutorial we'll take a look at what classes are, and how we can use them to create new objects.
----------------------------------------
🐱💻 🐱💻 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 AI Lessons
⚡
⚡
⚡
⚡
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · AI
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · ChatGPT
How AI Assist Turns a Rough Draft into a Polished Document in Minutes
Dev.to · paperquire
🎓
Tutor Explanation
DeepCamp AI