What to expect in the next generation of Laravel Forge. Read the blog post
Rewatch this lesson

Next video in… 10

Modern PHP

Classes

PHP Fundamentals

Classes
Get started with object-oriented programming in PHP, learning about classes, properties, and methods with real-world examples.


Classes are a fundamental part of modern PHP development. Think of a class as a blueprint - it defines what properties (data) and methods (functions) your objects will have. Objects in PHP help you organize related data and functionality together in a reusable way.

Basic Class Structure

Let's start with a simple example:

1class Product
2{
3 public string $name;
4 public float $price;
5 
6 public function isExpensive(): bool
7 {
8 return $this->price > 100;
9 }
10}
11 
12$product = new Product();
13$product->name = "Phone";
14$product->price = 99.99;
15 
16echo $product->name; // returns “Phone”
17var_dump($product->isExpensive()); // returns false

This simple example shows the basic components of a class: • Properties ($name, $price) store data

• Methods (isExpensive()) define behavior

• The $this keyword refers to the current object

Constructor Method

We can make our class better by using a constructor - a special method that runs when creating a new object. It lets you add data while creating a new object instance:

1class Product
2{
3 public function __construct(
4 public string $name,
5 public float $price
6 ) {}
7}
8 
9$product = new Product("Phone", 99.99);

Access Levels

PHP provides three visibility levels for properties and methods:

1class Product
2{
3 public string $name; // Accessible from anywhere
4 private float $price; // Only inside this class
5 protected bool $active; // Inside this class and child classes
6}

Inheritance

Classes can inherit properties and methods from other classes. This is useful when you have similar types of objects that share common features but need some additional functionality. Instead of duplicating code, you can extend a base class:

1class Product
2{
3 public function __construct(
4 public string $name,
5 public float $price
6 ) {}
7 
8 public function hasDiscount(): bool
9 {
10 return $this->price < 100;
11 }
12 
13 public function getDescription(): string
14 {
15 return "{$this->name} costs {$this->price}€";
16 }
17}
18 
19class DigitalProduct extends Product
20{
21 public function __construct(
22 public string $name,
23 public float $price,
24 public string $downloadLink
25 ) {}
26 
27 public function download(): string
28 {
29 return "Downloading {$this->name} from {$this->downloadLink}";
30 }
31}
32 
33$course = new DigitalProduct('PHP Course', 99.99, 'course.zip');
34echo $course->getDescription(); // inherited method
35echo $course->hasDiscount(); // inherited method
36echo $course->download(); // new method

When you start working with Laravel, you'll use classes extensively. Every model, controller, and service in Laravel is a class. This foundation will help you understand Laravel's structure better.

00:00
Classes are a crucial part of modern PHP development. Think of a class as a
00:04
blueprint. It defines what properties and methods your objects will have and
00:09
objects in PHP help you to organize related data and functionality together
00:14
in a reusable way. We're going to start very simple by creating our first class
00:20
here. By the way we're still working here in this online editor. Normally for every
00:24
class you would have a separated file but we're going to take a look at this
00:28
in the later videos. We're also going to use an IDE in order to work on a PHP
00:34
project but we are still keeping it simple with our online editor. So the
00:39
class keyword is what we're going to start with and then we'll give our class
00:42
a name and I'm going to call this product. So this is our product class and
00:47
we have open and closing curly braces. That's basically already it to define a
00:53
class. We can already create it. We can instantiate it. That's what we call
00:58
when we create an object of a class. So we're going to store this and let's call
01:05
this product and we're going to create a new one with the new keyword then the
01:09
class of the name and then parentheses. Alright and let's just also dump this
01:14
out. Our first product, our first PHP object and here is nothing fancy about
01:21
this yet. It's empty because we haven't defined anything for this class yet but
01:26
yeah that's what we're going to do now. So again classes are very important
01:30
classes and objects in order to define and store some data and functionality
01:36
inside a class, inside a way that we can reuse and structure it. So we're going to
01:42
start by creating our first property. We have an access level. We're going to talk
01:47
about this in a bit but for now we're just saying this should be public and
01:51
available anywhere. Then we have our type. Let's start with a string because
01:56
we will give now our product a name. Okay so access level, the type and then the
02:03
name of this property. If we run again we can see our object has already a name
02:07
but nothing is in this name yet so we have to do this. So we can already talk
02:13
about something very special in PHP, a special method which is called the
02:17
constructor and we create this again with the public access level and now
02:21
we're creating a function and this is a special one with double underscore
02:26
called construct and you will see we have a lot of very special magical
02:30
methods in PHP which start with underscore just so you better know that
02:34
they are special and this is special because the constructors run every time
02:39
we create a new product. So currently if we're creating a new product this is
02:44
being run we don't see it yet but it is run under the hood and the most
02:48
important part of our constructor is to deal with data that we want to provide
02:52
to our classes. So let's say we want to create a new product and let's call this
02:58
maybe laptop and now we want to assign this string to this name which is then
03:03
stored inside our object. We can just do this by saying this is a name here and
03:09
then inside this body we have access to properties or access to this current
03:14
class with this so this always refers to our current product here inside this
03:19
object and then we say that the name so now we don't need the dollar sign
03:24
anymore we just need the name here and then we assign dollar name to our name
03:31
property. So if we run this you will see already the output is a little bit
03:34
different because now name already has here value which is a string which is
03:39
laptop which is what we have provided here. Awesome so we have created now our
03:43
first product here which is a laptop and it already has a name so and this should
03:49
be now an integer and this is the price of our product. So this means you want to
03:54
provide here next to name also a price let's say this is 100 probably dollars
03:59
and then we say we also expect the price to come in through the constructor and
04:05
then we're also going to define the value of this property by using the one
04:11
which we get here through the constructor. Okay so again we're providing
04:15
it here when we create our new class this gets to the constructor this is not
04:20
a second argument that's how we know this is the price and then we're going
04:23
to set this to our internal property. Alright let's take a look what we get
04:29
here yeah you can see now we have a property here which is available and
04:33
which has an integer value of 100. It's already a good idea to tell you that we
04:38
have a little nicer approach here to handling properties in PHP since
04:44
PHP 8 and the better way we can define this is we don't define them here
04:49
outside we can define our properties or maybe let's bring them back here we can
04:54
define them inside our constructor. So let's give us some space here and I'm
05:00
going to paste in here our properties we can do this now and now we only need
05:06
commas here to separate them and we don't need to assign them here
05:10
inside the body. So this is now what a modern constructor will look like in PHP.
05:16
So we're defining through the constructor not only the parameters but
05:20
we also define their access level again we will talk about this in a bit but
05:24
also the type. So this means this should still work the same and it does and this
05:29
is now way nicer and cleaner syntax in order to work with constructor and
05:33
properties in your PHP classes. And of course we can also access these values
05:40
here directly so let's echo out now our product's name. We do this with the arrow
05:47
operator and this works or the same goes with the price. Yes this also works.
05:54
Alright now let's create our own first function and we do this very similar to
05:58
constructor we have an access level public then a function and then the name
06:02
and this will be one to define if our product is expensive or not and I'm also
06:08
going to use here the return type I'm saying this should be boolean and the
06:14
way we define this very simple here we have access to the price again through
06:18
this and the arrow operator and we're going to check if the price is bigger
06:23
than 100 and if it is then we're going to say this is expensive so let's use
06:29
vardump here again and now we're accessing a method which is very similar
06:33
to a property so it is expensive but now we have parentheses here which tells us
06:40
that this is a method and we get back false back let's give us some space here
06:45
and if we make this 101 it should be true and yeah now this is expensive and
06:51
that's how you can access your properties or methods on your own PHP
06:55
classes. Cool let's go briefly back to the access level so for now we have set
07:01
everything here to public so for properties and for functions or methods
07:06
methods is just a more fancy name for functions that belong to your class they
07:12
have an access level and this defines where and how you can call them
07:16
currently we have set them to public so this means you can run them from
07:19
everywhere like here from outside this object but if you would change this to
07:26
protected this will not fail because call to protected method which is not
07:32
allowed in this case and the same would happen with the third access level which
07:37
we have which is private but let's bring this back here so this is working again
07:42
yeah so public you're going to use if you want to make your properties and
07:46
methods accessible from everywhere if you use protected you can only use them
07:51
from within the same class and extended classes we're going to talk about
07:56
extending class in a bit and with a private if you have a private method
08:00
changes again like this then this method will be only accessible from within this
08:07
product so I can't access it from the outset anymore but yeah you often also
08:11
have methods that you're just going to use inside your class but you will learn
08:15
more about this the more you start working with PHP let's get back to
08:19
public here for example okay so I mentioned inheritance and extending
08:24
another class let's get rid of this and let me bring in here another product
08:29
class okay there's another product class very similar to what we had before we
08:33
now have a constructor here we have a has discount and get description method
08:38
let's just create this product here we'll create new product and let's dump
08:43
this out just so we see that this is working and of course it's not because
08:49
we need to provide here a name laptop and a price yeah okay now it's back
08:57
working okay so far so good so sometimes you end up with a product like laptop
09:04
here but then later on your product class is working but then you I'm
09:08
realizing that you have different kind of product so let's say you have a
09:12
laptop but maybe you also have an application now this is a digital
09:17
product it doesn't make any difference in this case but let's say a digital
09:22
product has a different method so for example a digital product might have a
09:27
link so let's add here new method get link we're going to return a string and
09:32
we're just returning here the application link normally you might get
09:38
this from a database or somewhere else but we're just providing here string and
09:41
now let's say we want to get the link of this product and this works but of
09:48
course if we go back to our laptop example now our laptop has also a method
09:54
called get language yeah it doesn't really make sense because this is
09:57
something that would be only useful for digital products and this is a use case
10:02
where inheritance makes a lot of sense so let's get rid of this and what we're
10:06
going to do is now we're going to create another class and this is called
10:10
digital product all right and now what we're doing here is we're going to bring
10:15
in our method here get link okay cool and now we're creating here a digital
10:21
product again this is our app and everything should still work and it does
10:26
but of course if we try now to get one of those other methods here and this
10:32
will not work anymore because this get description method is not part of our
10:36
digital product it's part of the default product class so now what you
10:40
could do is you could copy this get description method and put it inside a
10:44
digital product as well but of course now you end up with duplicated code
10:48
which is not what we want so what we can do in PHP we can say you want to extend
10:52
our product and now you will see this is working again we have to get
10:58
description we have to was it still is expensive I believe let's try it out no
11:05
it was it or it has discount that's the one which I wanted to try false yeah so
11:11
this also works so this is not very good example of using inheritance where we
11:15
just say the product class is our parent class and the digital product is our
11:20
child class we want to reuse all the functionality of a product but we want
11:25
to add some specific features which are only accessible or should be only
11:29
accessible for digital products in this case and then you're going to probably
11:34
end up with a bunch of more specific products which which have some specific
11:39
features and this is a good way to separate those concerns and yeah that's
11:44
why inheritance is very powerful inside of PHP alright so far so good we talked
11:49
about how you can create your first class how you can define your
11:52
constructor the parameters the data that it will receive how you can store this
11:56
inside your class use functions or methods in this case but also we talked
12:02
about extending a class and using inheritance okay but again this is just
12:08
a start when we talk about classes and objects but it's a solid foundation
12:13
which you will need for our next videos and also for working with level which of
12:17
course is our end goal here