Create a Basic PHP Class

Video loading...

  • 0:01
    Create a Basic PHP Class
  • 0:03
    with Leanna Pelham
  • 0:05
    Let's create a fresh file that we can play around with.
  • 0:10
    Call it play.php.
  • 0:13
    Now we can warn the rebels that it's a trap.
  • 0:20
    Put play.php in the URL.
  • 0:22
    And there it is.
  • 0:23
    We've conquered the echo statement.
  • 0:27
    Now to the cool stuff.
  • 0:29
    The first super important, big awesome, crazy thing in object
  • 0:32
    oriented programming is a class.
  • 0:36
    To create one, write the keyword class, then the name of it.
  • 0:40
    This name can be almost anything alphanumeric.
  • 0:45
    Finish things off with an open curly brace and a close curly brace.
  • 0:49
    Nice work.
  • 0:50
    Don't worry about what this class thing is yet.
  • 0:54
    But if you refresh, you can see that creating a class
  • 0:57
    doesn't actually do anything.
  • 1:02
    Creating a class, check.
  • 1:05
    And with that, we can see the second super
  • 1:07
    important, big awesome, crazy thing in object
  • 1:10
    oriented programming, an object.
  • 1:13
    Once you have a class, you can instantiate
  • 1:16
    a new object from that class.
  • 1:18
    And it looks like this.
  • 1:21
    Create a variable called myShip.
  • 1:23
    And then use the new keyword followed by the name of the class,
  • 1:27
    then open parentheses, close parentheses.
  • 1:30
    It kind of looks like we're calling a function called Ship,
  • 1:33
    except for the new keyword in front of that
  • 1:35
    tells PHP that Ship is a class and we're
  • 1:38
    instantiating a new object from it.
  • 1:41
    Before we explain any of this, refresh again.
  • 1:45
    Still no changes.
  • 1:47
    We have this new thing called an object that's set to myShip.
  • 1:51
    But it doesn't cause anything to happen.
  • 1:54
    OK, let me explain this class and object stuff.
  • 1:58
    When this stuff finally clicks, you'll know that you really
  • 2:01
    get object-oriented programming.
  • 2:04
    So listen carefully.
  • 2:05
    And I'll come back to this later as we add more stuff.
  • 2:08
    Pretend with me.
  • 2:09
    You're the manager of a shipping dock on the Death Star.
  • 2:13
    To avoid any force choking, when each ship lands,
  • 2:16
    you need to take an inventory of it.
  • 2:18
    What's its name, size?
  • 2:21
    Does it have a warp drive?
  • 2:22
    What's the fuel level, weapon power, defence strength, and other stuff?
  • 2:28
    And also imagine that even though you're in a flying space death
  • 2:31
    machine, you don't have computers.
  • 2:33
    You track everything by making copies of a template worksheet
  • 2:37
    you designed in Excel.
  • 2:39
    Yeah, they use Windows on the Death Star.
  • 2:41
    A class is like that empty template, with blank lines for the ship's
  • 2:45
    name and size, a yes/no option for warp drive,
  • 2:48
    and blanks for fuel level, weapon power, and defense strength.
  • 2:52
    It's not actually a ship, of course.
  • 2:54
    But it defines all the properties that a ship might have.
  • 2:59
    OK, I think you've got the gist on classes.
  • 3:02
    Now let's go over objects.
  • 3:04
    This is where I'm supposed to tell you
  • 3:06
    to think of an object like a real ship that's landed in our dock.
  • 3:11
    That's correct, but I think it's closer
  • 3:14
    to think of an object like a completed worksheet
  • 3:17
    that we've filled out for a ship, complete with its name, weapon
  • 3:21
    details, strength, and fuel levels.
  • 3:24
    If 10 ships land, then we'll print out 10 blank worksheets
  • 3:28
    and fill each in with different details.
  • 3:31
    If we refuel one of those ships, we'll update
  • 3:33
    the fuel level on its worksheet.
  • 3:36
    But each ship is using the same template or a class.
  • 3:40
    If we wanted to also track a ship's weight,
  • 3:42
    we'd need to go back to the template and add a field for it there.
  • 3:47
    Right now, our class is empty.
  • 3:49
    That's like a template with no blank fields to fill in, not helpful.
  • 3:54
    Time to fix that.
  • 3:56
    OK, clear your mind quickly and think about arrays.
  • 3:59
    An array holds data on keys.
  • 4:02
    We choose a key like has warp drive, then put something there.
  • 4:06
  • 4:07
    An object works exactly like an array,
  • 4:10
    except instead of calling these storage spaces keys,
  • 4:13
    we call them properties.
  • 4:15
    But basically, they work the same way.
  • 4:18
    There is one big difference between an array and an object.
  • 4:23
    With an array, you can just invent a new key and set data on it.
  • 4:27
    But with an object, you need to pre-register
  • 4:29
    the possible properties it might have in its class.
  • 4:33
    Back at our loading dock, this means an array is like a template where
  • 4:37
    each line has two blanks, one for the value
  • 4:40
    and one for what the value is.
  • 4:43
    We might fill in a line with the ship's name but not the fuel level.
  • 4:47
    And maybe we'll write down the color of the ship on another line.
  • 4:50
    And because I don't really like my job,
  • 4:52
    I'll put my current favorite song on the last line.
  • 4:55
    If I give 10 sheets to my manager, she'll
  • 4:57
    have no guarantee of what data I may or may not have recorded.
  • 5:01
    Simple, but unstructured.
  • 5:04
    But an object is like the sheets we were talking about earlier.
  • 5:08
    It has a list of exactly what we want to track with a blank next
  • 5:11
    to each for that value.
  • 5:14
    So if we want to be able to store the name of each ship,
  • 5:17
    we'll need to add a spot for it on the template.
  • 5:20
    In object-oriented land, we need to add
  • 5:22
    a name property to our ship class.
  • 5:25
    Let's do it.
  • 5:26
    Say public, then $, then the name of your property.
  • 5:30
    In this case, our name is actually name.
  • 5:34
    This doesn't do anything.
  • 5:36
    But now ship objects are allowed to hold data on a name property.
  • 5:40
    And how do we set that?
  • 5:42
    We do it with this syntax here.
  • 5:50
    Now they can read the data from that property using the same syntax.
  • 5:58
    Try it out.
  • 6:01
    It's working great.
  • 6:04
    Does this feel familiar?
  • 6:05
    It works exactly like an array, except instead
  • 6:09
    of the square bracket syntax, we use this -> syntax.
  • 6:13
    Let's call that an arrow.
  • 6:16
    The real difference between an object and an array
  • 6:19
    is that an object has a class that defines all possible properties
  • 6:23
    that it can hold, instead of being able to store any random keys you
  • 6:27
    dream up, like an array.
  • 6:29
    And with this simple difference, you get a huge benefit.
  • 6:34
    A class is like programmer documentation.
  • 6:37
    If I give you an array, you have no idea what data is on it.
  • 6:42
    You can var_dump it to see, then just hope that it will always
  • 6:45
    have the same keys in the future.
  • 6:48
    But if I hand you a ship object, you know something about it.
  • 6:52
    You know that it will always have a name property.
  • 6:55
    And it'll never have email or phone properties,
  • 6:58
    because they're not in the class.
  • 7:01
    The class gives us a skeleton and some rules we know it'll follow.
  • 7:05
    Now let's talk about something even better than this, methods.

Create a Basic PHP Class


This lesson covers the basics of classes and objects. You'll learn how to set up a class and then what a class is and what objects are like. By the end of this lesson, you should be able to create a class, an object, create a property, and set the value of a property inside a class.

Log in or sign up to download companion files.
Additional resources: