TubeSum ← Transcribe a video

PHP OOP - Beginners Crash Course

0h 24m video Transcribed Jun 30, 2026 Watch on YouTube ↗
Intermediate 12 min read For: Intermediate PHP developers or beginners with basic PHP knowledge who want to learn object-oriented programming concepts.
164.0K
Views
2.8K
Likes
94
Comments
56
Dislikes
1.8%
📊 Average

AI Summary

This video provides a beginner-friendly crash course on Object-Oriented PHP, covering fundamental concepts like classes, properties, methods, access modifiers, constructors, inheritance, abstract classes, and static members. The presenter assumes viewers have basic PHP knowledge or experience with other OOP languages, and demonstrates each concept with simple code examples.

[1:03]
Defining a Class

A class is defined using the `class` keyword followed by the class name (e.g., `Customer`). It serves as the building block of OOP in PHP.

[1:35]
Properties and Methods

Classes contain properties (attributes) and methods (functions). Properties are declared with access modifiers (`public`, `private`, `protected`). Methods are functions inside a class.

[2:11]
Access Modifiers

Three access modifiers: `public` (accessible from anywhere), `private` (accessible only within the class), and `protected` (accessible within the class and its subclasses).

[3:36]
Instantiating an Object

Create an object using `new ClassName()`. Access public properties with `$object->property` and call methods with `$object->method()`.

[7:34]
Constructor and Destructor

The `__construct()` method is called automatically when an object is instantiated. `__destruct()` runs when the object is destroyed. These are magic methods.

[13:47]
Inheritance with `extends`

A class can extend another class using `extends`. The child class inherits properties and methods from the parent. Use `parent::__construct()` to call the parent constructor.

[17:54]
Abstract Classes and Methods

An abstract class cannot be instantiated directly. Abstract methods have no body and must be defined in child classes. Declare with `abstract class` or `abstract function`.

[20:35]
Static Properties and Methods

Static members belong to the class, not instances. Use `self::` to access them inside the class. Call externally with `ClassName::method()` (e.g., `User::getPasswordLength()`).

This crash course covers the essential OOP concepts in PHP—classes, properties, methods, access modifiers, constructors, inheritance, abstract classes, and static members—providing a solid foundation for building object-oriented applications.

Clickbait Check

85% Legit

"The title accurately reflects the content: a beginner crash course on PHP OOP fundamentals, though it lacks a promised 'part two' application example."

Mentioned in this Video

Tutorial Checklist

1 1:03 Define a class using the `class` keyword (e.g., `class Customer {}`).
2 1:40 Declare properties inside the class with access modifiers (e.g., `public $id;`).
3 3:56 Define methods (e.g., `public function getCustomer($id) { ... }`).
4 5:36 Instantiate the class with `$customer = new Customer();`.
5 6:01 Access public properties and methods (e.g., `$customer->id` or `$customer->getCustomer(1)`).
6 7:34 Add a constructor using `public function __construct(...) { $this->property = $param; }`.
7 12:56 Make properties private and create public getter methods (e.g., `public function getEmail() { return $this->email; }`).
8 14:21 Create a child class using `extends` (e.g., `class Subscriber extends Customer`).
9 15:36 In the child constructor, call `parent::__construct(...)` and set additional properties.
10 17:54 Make a class or method `abstract` to define a base that cannot be instantiated directly.
11 20:35 Declare static properties/methods using `static` keyword and access with `self::` or `ClassName::`.

Study Flashcards (10)

What keyword is used to define a class in PHP?

easy Click to reveal answer

class

1:15

What are the three access modifiers in PHP?

easy Click to reveal answer

public, private, protected

2:11

What is the difference between private and protected?

medium Click to reveal answer

Private: accessible only within the class. Protected: accessible within the class and subclasses.

2:23

How do you access a property inside a method of the same class?

easy Click to reveal answer

Using `$this->propertyName` (e.g., `$this->id`).

4:41

What special method is called automatically when an object is instantiated?

easy Click to reveal answer

__construct()

7:37

What is the syntax to call a parent constructor from a child class?

medium Click to reveal answer

parent::__construct(...)

15:36

Can an abstract class be instantiated directly?

medium Click to reveal answer

No

17:54

What keyword is used to access a static property inside a static method of its own class?

medium Click to reveal answer

self::

22:26

What is the syntax to call a static method from outside the class?

medium Click to reveal answer

ClassName::staticMethod() (e.g., User::getPasswordLength())

23:00

What happens if you try to access a private property from outside the class?

hard Click to reveal answer

You get an error: 'cannot access private property'.

6:31

💡 Key Takeaways

⚖️

Classes are the building blocks of OOP

Foundational concept emphasized as the core of object-oriented programming.

1:03
📊

Three access modifiers explained

Clear distinction between public, private, and protected with practical implications.

2:11
🔧

Best practice: public methods, private properties

Key encapsulation principle demonstrated with getter methods.

12:56
⚖️

Abstract classes cannot be instantiated

Important OOP design pattern explained with concrete example.

17:54
💡

Static members belong to the class, not instances

Distinction between instance-level and class-level members clarified with password length example.

20:35

✂️ Creator Tools: Viral Hooks

AI-generated clip ideas for Shorts based on the transcript

PHP Access Modifiers: Public vs Private vs Protected

50s

Clearly explains three crucial visibility levels with live error demonstrations, essential for avoiding bugs.

▶ Play Clip

PHP Magic Methods: Constructor & Destructor Explained

60s

Shows how constructors and destructors automatically run, with visible output that proves the concept.

▶ Play Clip

Why Private Properties + Public Methods = Best Practice

45s

Demonstrates encapsulation by making properties private and using getter methods, a core OOP principle.

▶ Play Clip

PHP Inheritance: How to Extend a Class

60s

Teaches code reuse with parent::construct, showing how subclasses can inherit and extend functionality.

▶ Play Clip

PHP Static: When You Don't Need an Object

60s

Explains static properties and methods with a practical password length example, clarifying a common source of confusion.

▶ Play Clip

[00:00] hey guys this video is going to be on

[00:03] object orientated PHP all right so if

[00:07] you do not know any PHP at all I would

[00:09] suggest that you at least learn the the

[00:12] very Basics uh at least things like

[00:15] variables arrays functions Loops just

[00:18] just the the the real fundamentals of

[00:22] PHP all right uh if you if you don't

[00:25] know PHP but you have experience in

[00:27] another language especially if it it's

[00:29] if it's an object orientated language

[00:32] then you should be all right all right

[00:34] uh what what we're going to be doing is

[00:35] going to be pretty simple so if you

[00:37] already know uh object-orientated PHP

[00:40] then you probably don't want to continue

[00:42] because it's it's probably just stuff

[00:44] you already know um but feel free to

[00:46] watch and and maybe use it as a

[00:48] refresher all right so what I have is I

[00:51] already have my server set up I'm using

[00:53] zamp or xamp and I have my local host at

[00:58] local. and then I just have a index PHP

[01:01] file so the first thing we're going to

[01:03] go over is classes okay classes are the

[01:08] basically the building block of

[01:10] object-oriented programming at least in

[01:12] PHP all right so to define a class you

[01:15] just want to use the word class and then

[01:17] whatever you want to call it in this

[01:19] case I'm going to call it customer all

[01:21] right and I haven't already planned out

[01:23] this code just to let you know I'm just

[01:25] kind of uh freestyling it all right so

[01:28] we may have a couple errors or or

[01:30] whatever but um it'll be fine all right

[01:33] so we have a class called customer and

[01:35] then in a class you have two core

[01:37] components that's properties and methods

[01:40] properties are basically just attributes

[01:42] of the class all right so we have a

[01:44] customer class so we may have something

[01:47] like um an ID let's say uh a

[01:55] name

[01:58] email um I D name

[02:01] email and let's

[02:04] say

[02:06] location okay now when we're using PHP 5

[02:11] or newer which most likely you are you

[02:14] want to you want to use what's called an

[02:16] axis modifier all right also sometimes

[02:19] called an axis identifier and there's

[02:21] three different ones you can use okay

[02:23] there public which means that and I'm

[02:26] going to go over this more in depth in a

[02:28] little bit but public just means that

[02:30] you can access this this property from

[02:32] outside of this class all right we also

[02:35] have private okay that's going to make

[02:38] it so you can't access this anywhere

[02:41] except this class okay you can't access

[02:43] it outside of it you can't access it

[02:46] from another class even if it's extended

[02:48] from this one all right and then you

[02:50] have

[02:52] protected which means that you can't

[02:55] access this from

[02:56] outside but you can access it from

[02:59] another class class that extends

[03:01] customer all right so for example if we

[03:03] have let's say

[03:05] class um my

[03:08] class

[03:10] extends customer okay if we do that then

[03:13] we'll be able to access email from in

[03:15] that class all right so hopefully that

[03:18] makes sense uh but what I'm going to do

[03:20] is I'm going to set all these to public

[03:22] for

[03:27] now okay and we'll get location

[03:33] all right so now we have some properties

[03:34] for our customer

[03:36] class actually you know what instead of

[03:38] location let's use

[03:40] balance okay since they're customers

[03:42] they probably have some kind of

[03:45] balance now we're just declaring these

[03:48] declaring these but you can also set a

[03:50] default okay if we wanted to say ID

[03:52] equals 1 we could certainly do

[03:54] that okay

[03:56] and next thing is methods methods are

[04:00] functions so try not to get confused

[04:02] with methods and functions all a method

[04:04] is is a function that's inside of a

[04:06] class all right so you can also use uh

[04:10] access modifiers on that as well so

[04:12] we'll say public function and let's

[04:16] say uh get

[04:20] customer all right now this most likely

[04:22] would take in an

[04:25] ID okay so when you call this you would

[04:27] you would put the customer ID in

[04:31] and then most likely what you would do

[04:32] is you would take the ID that's passed

[04:34] in and you would assign it to the

[04:37] property ID so to do that what we would

[04:39] do is say

[04:41] this ID equals ID all right so if you

[04:45] ever want to access a property in your

[04:47] class you could use the the keyword this

[04:51] and then you want that and then ID all

[04:54] right or whatever the property is all

[04:56] right that way you can access these from

[04:58] every method in your class

[05:00] all right because if we just keep it as

[05:02] ID and get customer we we can't access

[05:05] this from anywhere

[05:06] else now uh a method like get customer

[05:09] what it would probably do in a real

[05:12] application is it would go and fetch a

[05:15] customer from the database with that

[05:17] particular ID all right and then you

[05:19] would probably just return the customer

[05:21] so we don't have the database part but

[05:23] what we can do is just say return and

[05:26] let's say it grabs a user called John do

[05:31] all right our customer called John

[05:33] do so now I'm going to show you how we

[05:36] can instantiate this class inside of our

[05:39] PHP all right so we want to make sure

[05:41] that we go outside of the class now all

[05:43] right now to instantiate it you're going

[05:45] to create a variable let's say customer

[05:49] and we want to set it to New and then

[05:52] the name of the class so new customer

[05:55] all right and that gives us a new

[05:57] customer object or instance all right

[05:59] right and then we can continue and we

[06:01] can actually access public properties

[06:04] and and public methods or functions all

[06:07] right so for instance uh let's say that

[06:10] we want ID to be one by default then we

[06:13] can go and we can

[06:15] Echo customer

[06:18] ID save it reload and we get one now

[06:23] just to demonstrate these uh the

[06:25] visibility or the axis modifiers here

[06:28] let's make this

[06:31] private okay and if we go and reload

[06:34] we're going to get this cannot access

[06:36] private property okay if I change it to

[06:41] protected then it's saying cannot access

[06:44] protected property all right so let's

[06:47] make it back to public and get rid of

[06:49] that okay now if we want to call a

[06:51] method we can do the same thing we're

[06:53] going to Echo customer and let's say get

[06:57] customer and then we need to pass in an

[06:59] ID

[07:00] all right uh so if we go ahead and

[07:03] reload we get John Doe because that's

[07:05] what we're returning here all right if

[07:07] we want to return the ID we could do

[07:11] that okay so let's pass in

[07:15] 11 and reload and we get 11 all right

[07:20] and then the same thing if I set this to

[07:24] private we get the error okay same thing

[07:26] with

[07:28] protected all right so that takes care

[07:31] of properties and methods now we also

[07:34] have a method called construct okay

[07:37] which is a Constructor and there's a lot

[07:40] of different programming languages that

[07:41] use Constructors what it is is it's a

[07:44] special function that or method that is

[07:47] going to run when we instantiate the

[07:50] object so when we run this if we have a

[07:53] construct function that's going to go

[07:55] ahead and run all right so let's create

[07:57] one we'll say public function and then

[08:00] you want to use double underscore and

[08:03] then

[08:06] construct okay just like that and just

[08:09] to test it out we'll say

[08:12] Echo uh we'll say

[08:15] the

[08:17] Constructor

[08:20] ran all right so now let's save and all

[08:23] we're doing here is instantiating the

[08:25] customer object okay and you can see the

[08:28] Constructor ran

[08:30] and we also have another one and these

[08:32] are called Magic methods okay and

[08:33] there's a bunch of them I'm going to go

[08:35] over some more in a little bit but

[08:37] construct and and um destruct are magic

[08:41] methods all right so let's say public

[08:47] function

[08:50] destruct and uh we'll do the same thing

[08:53] we'll just

[08:54] Echo we'll say we'll say destruct

[09:00] uh Destructor

[09:04] ran all right so if we reload you can

[09:08] see we get the Constructor ran and then

[09:09] the destructor ran all right and if we

[09:13] go ahead and spit out the customer again

[09:15] let's actually just return the

[09:20] name okay when we run this it should go

[09:23] in between the construct and destruct

[09:26] functions all right so uh let's say

[09:29] custom

[09:30] customer

[09:32] get customer and just put in 10 okay now

[09:36] if we

[09:37] reload uh oops we got to Echo

[09:42] it okay so now you can see that John Doe

[09:45] is being printed out in between the

[09:47] two now common practice would be if

[09:50] we're going to just get a customer here

[09:53] and we're going to pass in um an

[09:55] attribute or property and then assign it

[09:58] like this we can do that in the

[10:00] construct all right so it would take

[10:02] away the need to have this really all

[10:05] right so um unless you're dealing with

[10:08] the database and all that you would do

[10:10] it but if we're just assigning uh class

[10:13] properties then you want to do that in

[10:15] the construct all right so in here let's

[10:17] say

[10:18] ID uh

[10:21] name

[10:25] email and balance okay and then Let's uh

[10:29] let's copy that and we'll just paste

[10:33] that

[10:34] here okay so we get ID then we'll do

[10:40] name this will be

[10:47] email and

[10:53] balance all right and I like to make

[10:57] these nice and neat

[11:02] okay and then we can get rid of this all

[11:05] together okay and we don't need this

[11:07] destruct

[11:09] either and we'll get rid of this all

[11:12] right so what we can do now is when we

[11:15] instantiate the customer we want to

[11:17] actually pass in the values here all

[11:19] right so the ID let's say

[11:22] one then the name we'll say my

[11:27] name and then an email Ma say Brad at

[11:33] Gmail and then a balance let's just say

[11:37] zero okay so if we want to Output any of

[11:41] these we can now just say

[11:44] Echo

[11:46] customer and let's Echo out the

[11:50] name okay and that's going to give us

[11:53] the name so now we have this customer

[11:55] that we can work with if we wanted to

[11:58] create a function

[11:59] [Music]

[12:01] function called let's say um get

[12:07] email all right and then we can

[12:10] just

[12:11] [Music]

[12:12] return

[12:14] this email all right and then instead of

[12:18] doing this well let's get rid of that

[12:21] and say we want the

[12:25] email and that gives us the email and

[12:27] notice we don't have to pass anything in

[12:29] here we don't have to pass in an ID or

[12:31] anything because we have our customer

[12:33] right here it's an instance it's just

[12:35] talking about this one customer all

[12:38] right so we can continue to uh run

[12:41] methods on that

[12:43] customer now the reason that you would

[12:45] want to do this is because uh you could

[12:49] keep your properties all private and

[12:51] then you could just have a method that

[12:53] gets that private property okay so let's

[12:56] make all these

[12:57] private and this is really common

[13:04] practice okay so if we go ahead and save

[13:07] that reload we're not getting any error

[13:10] and it's letting us get the email

[13:11] address because we're not accessing it

[13:13] directly okay we're not saying customer

[13:16] email okay if we do

[13:20] that we're going to get that error okay

[13:23] because what we're doing is we're using

[13:24] this method here to get the private

[13:27] property all right and and this is

[13:29] public I mean if I set this to let's say

[13:33] protected and reload we're going to get

[13:35] an error okay so public methods private

[13:39] properties is uh is really good practice

[13:43] so let's put that back all right so now

[13:47] let's talk a little bit about

[13:49] inheritance okay so as I said you can

[13:52] have a class extend another class so

[13:55] what I'll do is let's um

[14:00] let's comment this out for now and we're

[14:03] going to create another

[14:05] class

[14:07] um so let's say class and normally you

[14:11] would have these in their own files okay

[14:13] you'd have a customer for instance a

[14:15] customer. PHP and then um whatever else

[14:19] you have for classes in this case let's

[14:21] call it this uh

[14:25] subscriber okay so subscriber is going

[14:29] to extend customer so we want to say

[14:32] extends okay make sure it's plural

[14:35] extends

[14:38] customer okay so now we have two classes

[14:42] now in

[14:43] subscriber let's create its own property

[14:47] okay so we'll say

[14:49] public and let's say there's a property

[14:52] called plan okay this is a subscriber

[14:55] and they're on a specific plan okay and

[14:58] plan is not part of Customer because not

[15:00] all customers are

[15:02] subscribers now when we instantiate a

[15:05] subscriber we want to do the same thing

[15:07] that we we did with customer it we want

[15:09] to pass these in all right but we don't

[15:11] want to

[15:12] repeat um having to put all these down

[15:16] here and these um so what we'll do is

[15:19] let's create our construct I'm going to

[15:22] copy this

[15:23] one all right now we're going to have to

[15:26] pass in all the stuff ID name email

[15:28] balance

[15:29] but we don't want to have to do this

[15:31] again all right so let's get rid of that

[15:33] and what we can do here is we can

[15:36] specify that we want the parent

[15:37] construct to run okay so to do that

[15:40] we're going to say parent and then a

[15:41] double colon and

[15:44] then

[15:46] construct okay just like that and then

[15:48] we want to pass these

[15:50] in all right now when we instantiate a

[15:53] subscriber we also want to put in the

[15:55] plan all right so we want to make sure

[15:57] we put that here but we don't want to

[15:59] pass it to the parent because there is

[16:01] no plan up here okay it's not a um it's

[16:05] not a customer property it's a

[16:06] subscriber property so what we'll do is

[16:09] down here we'll say this plan is going

[16:12] to be equal to the plan that's passed in

[16:14] all right and that'll take care of that

[16:17] and now what we can do

[16:19] is let's take the get

[16:23] email and cut that

[16:26] out and we'll paste it in here

[16:29] here all right so now we have subscriber

[16:32] get email and we want to make sure that

[16:35] the email up here in the properties is

[16:37] set

[16:38] to protected okay because that means we

[16:41] won't be able to access it from outside

[16:43] unless it's it's a class that extends

[16:46] customer okay so now what we can do is

[16:49] copy that make sure you're outside of

[16:52] both classes and this is going to be now

[16:57] subscriber he's going to equal new

[17:01] subscriber and now we can actually pass

[17:04] in the plan as well okay so we'll say

[17:08] Pro all right and then we should be able

[17:10] to call get

[17:13] email so

[17:17] subscriber and we'll say get

[17:22] email actually we need to Echo it

[17:27] out all right and now now we can get our

[17:30] email okay through

[17:33] subscriber now if we wanted the get

[17:35] email to actually stay in the customer

[17:40] class we can do that because since

[17:44] subscriber extends customer we have

[17:46] access to this all right so we can

[17:48] actually save that and that's going to

[17:51] do the same thing all right now next

[17:54] thing I want to talk about is abstract

[17:57] classes all right and abstract methods

[18:00] now when you have an abstract class it

[18:02] can't be instantiated okay so if we make

[18:05] customer abstract we can't do this down

[18:08] here we have to just use classes that

[18:10] extend it all right so let's take a look

[18:13] at that so we'll

[18:16] say abstract class customer all right

[18:19] and just to show you what it does let's

[18:23] comment this stuff out and then we'll

[18:24] uncomment that and reload and you get

[18:28] cannot instantiate abstract class

[18:31] customer

[18:33] okay so let's comment that back

[18:37] out and then let's uncomment this and

[18:41] let's try to call get

[18:45] email all right

[18:48] so that's going to work but if we want

[18:50] to make let's I just want to show you if

[18:53] we take away the abstract in the class

[18:55] and add it

[18:57] here onto a method and then we try it's

[19:01] telling us that get

[19:03] email uh can't be run and the reason for

[19:07] that is when you have an abstract method

[19:09] like this it needs to be in an abstract

[19:11] class okay so we need to put that back

[19:17] here okay and then

[19:20] reload uh wait a minute

[19:24] here Echo get subscriber

[19:27] email cannot contain

[19:32] body um

[19:34] oh that's because when we have our

[19:37] function here we can't actually um

[19:40] include a body all right so what we'll

[19:41] have to do is copy that and then this is

[19:45] we're just going to declare it like that

[19:47] all right and then down here is where we

[19:50] want

[19:51] to call it like

[19:55] that all right so you just want to

[19:57] Define your fun functions in the

[19:59] abstract class okay it's basically you

[20:02] can think of it as a base class that

[20:04] that you don't use directly but you

[20:07] extend uh classes from

[20:10] it all right so hopefully that makes

[20:13] sense um in my experience I haven't

[20:16] really needed to use abstract classes

[20:19] but um it's it's it's good to know it's

[20:21] a fundamental part of um object-oriented

[20:25] program all right so this is starting to

[20:27] get a little cluttered so I'm going to

[20:29] just get rid of all

[20:31] this and what I want to move on to now

[20:35] is the static keyword all right so you

[20:38] can set static properties and methods

[20:42] there you don't Define the class as

[20:44] static but the properties and methods

[20:46] inside of it you can all right so let's

[20:48] create a

[20:50] class

[20:51] um let's call it

[20:55] user and let's say user has just a

[20:59] regular uh property of

[21:04] username and

[21:07] also

[21:09] password okay now the purpose of using

[21:12] static classes and properties is that

[21:15] you don't have to create an instance you

[21:18] don't have to

[21:19] instantiate uh when you do that all

[21:21] right so let me just give you an example

[21:24] and we're going to say public static

[21:28] and let's say password

[21:31] length Okay and we're going to set that

[21:34] to a default of five all right so

[21:37] password length would be a good thing to

[21:39] to make static because you don't need a

[21:43] certain instance it's always going to be

[21:44] the same the username and password are

[21:47] going to be different depending on the

[21:49] user um but password length is going to

[21:51] be the same no matter what so that's a

[21:53] good example to use a static property

[21:57] all right and then we want to create

[21:58] let's say

[22:00] public um public

[22:04] static function and we'll say

[22:08] get password

[22:11] length all right and what we want to do

[22:16] is return now when you're dealing with

[22:19] static properties you you don't want to

[22:20] use this you don't want to do that what

[22:23] you want to use is the keyword self and

[22:26] then you want to use this double colon

[22:28] and and then the property okay so in

[22:30] this case it's going to be password

[22:37] length Okay so what we want to do now is

[22:40] let's go outside of the user outside of

[22:44] the user class and to call this we don't

[22:47] need to say um you know user equals new

[22:52] user or anything like that what we can

[22:54] do is just the name of the class and

[22:57] actually let's Echo out

[23:00] user and then same thing you want to use

[23:02] this double colon and then the function

[23:04] which is going to be

[23:06] get password

[23:09] length okay just like that let's go

[23:12] ahead and reload and it's giving us

[23:15] five all right and you would probably

[23:18] have this private but if it's public and

[23:19] you want to access it

[23:21] directly you can do that as

[23:24] well okay of course if I said private

[23:31] it's going to give us an error all right

[23:33] so that's how static uh static

[23:35] properties and static methods work all

[23:38] right so that's going to be it for now

[23:39] those are the the the very basic

[23:41] fundamentals that you need to know when

[23:44] working with object orientated PHP all

[23:47] right I will hopefully create a part two

[23:50] of this where we can actually create an

[23:52] application that's going to that's going

[23:54] to use um these Concepts all right so

[23:57] for now thank thanks for watching please

[23:59] subscribe leave a like leave a dislike

[24:01] if you didn't like it and I will see you

[24:03] next time

⚡ Saved you 0h 24m reading this? Transcribe any YouTube video for free — no signup needed.