Let’s get started by creating our first class and learn a little about what classes, objects, and instances really are. You’ll find that creating classes isn’t so hard.
Create a new file called pet.class.php. You don’t have to append the .class after your file name, but it’s common practice to do that so other developers know that this file contains a class and only a class. Classes should be separate in their own files and should be accessed from different files. With that being said, create a new file called index.php and then create a folder called classes. Place your pet.class.php inside that folder.
In your pet.class.php file enter this bit of code.
The keyword class is reserved by PHP and what it basically does is it allows us to create a class. It’s followed by the name of our class. This can be any name we want, but the first character must start with a letter or an underscore. The rest of the name can contain numbers, letters and underscores. The name of the class be anything we want as long as it doesn’t collide with a PHP reserved keyword. You can only have 1 class of the same name. If we had 2 Pet classes, then we would get an error.
This is all that’s needed to create a class. Of course, it really doesn’t do anything, but it works. Let’s open up the index.php file and add this bit of code.
include( 'classes/pet.class.php' );
The first thing we do is include our class so we can use it. Next, we create an instance of that class by using the new keyword. What’s an instance? Well an instance is basically a copy of a class. For example, if you open up notepad and then open up another notepad, you have 2 instances. It’s the same program, but you can use them differently and save different data. That’s what an instance is. It’s a copy of our class and we can use it however we like. If we had 2 instances of the Pet class and we used them differently, then anything we do to one won’t affect the other.
The new keyword allows us to create an instance and we specify what class we want an instance of. In our case, we want an instance of the Pet class. Right now, we’re just creating the instance, but we can’t access it after it’s creation. What if 100 lines of code later we wanted to access that class? How would we reference it? How would we be able to tell the difference between 2 instances of a class?
Update the line of code of where you create the instance of the class to this.
$jack = new Pet();
We now have created an object. What we’re doing is storing the instance of the Pet class and storing it in a variable. By doing this, we can access that instance and manipulate it any way we want. Like anything else, we can name our variable anything we want.
Classes, Objects, Instances!
You’ve probably never heard of those words before and don’t know what the difference is. A class is nothing but a blueprint of data. If we were building a house, then the blueprint for that building is just the design and not the actual house. In the programming world, we would call this a class. If we built multiple buildings based on that blueprint, then we would call these objects. A certain building is what we’d call an instance. An instance is how we would reference a certain building. Where as an object is just the building itself. Objects and instances can be interchangeable terms at times, but know the difference.
We’ve talked about the difference between all these terms and they might confuse you a bit. If you didn’t understand most of this, then don’t worry. You’ll get a lot of practice with this as we go along. Take a moment to take it all in before moving onto the next tutorial.