Classes in PHP

Classes in PHP

While arguments more dramatic than the Hatfield-McCoy feud continue to happen over “suggestions” for how you should style/format your PHP; I think we should get more people writing it. Everyone has a preference on how they like their code formatted, even I do, but learning the language is more important.

There are plenty of big words that we could use, but honestly, for the longest time, I didn’t know the proper names for a lot of things. Knowing the names helps communicate what you are doing or attempting to do, but it isn’t necessary.

This post is the first post of a series of posts that will eventually become a tutorial. In life, I prefer to take things one step at a time, as I do with programming, and I will do the same with my writing.

We will be working within one for to start and below is an example of a class and how to use it.


<?php
// Person.php

class Person {
    public $name;
    public $age;
}

// we create a Person class and assign it to a PHP variable
$levi = new Person;

// now we can add some information about this person
$levi->name = "Levi";
$levi->age = 32;

echo $levi->name;

Now we have a variable that contains my name and age. We can also create another variable for a different Person. We do the following in the same file.


// . . . 
$john = new Person;
$john->name = "John";
$john->age = 26;
// . . .

So we were able to use the Person class more than once, and we could continue to use it as many times as we wanted. The Person class is a blueprint; it represents the things we decide that make up a Person. Obviously, there are more things that make up a person, but we may not need them all in the app we are writing.

With classes you can also use a function called a construct, it is something that runs when you create the class. So when we do a new Person; the construct function runs. Instead of passing the name and age to the variables directly, we can use the construct to assign them. We can benefit from this behavior when we must have the name and age. Let’s rewrite the Person class as shown below.


<?php
// Person.php

class Person {
    public $name;
    public $age;

    // So we have these variables as arguements for our construct function
    public function __construct($name, $age)
    {
        // the line below assigns the name passed to the public $name
        $this->name = $name;

        // the line below assigns the age passed to the public age
        $this->age = $age;
    }
}

$levi = new Person("Levi", 32);

echo $levi->name; // this will echo out Levi

We will wrap things up with that. I encourage you to practice this, even if it seems silly, it will help. If you have questions, please email me, I am happy to help.

In an attempt to assist you with using the correct vernacular, let me cover the proper names of some of these items. A variable in a class is called a property of the class. A function inside a class is called a method. When you create a new class, you are instantiating it.

If you forget anything or break it; don’t beat yourself up. Take that opportunity to learn. Calmly figure out what happened and fix it. If you can’t figure it out right away, then you probably need a break.