In this section of the PHP tutorial we will be covering objects and classes in PHP.
PHP is something called an object-oriented programming language, which means that objects containing multiple different properties can be created. These objects allow us to bundle a bunch of data and functions together into one place. Multiple objects performing the same function, albeit with some minor differences, can also be created due to this property of PHP.
The reason that this is so important is that these objects are essential in using PHP, as they can cut down on the amount of coding that may have to be done. It also allows us to utilize the same code over and over and allows us to tweak it a little bit by changing the properties of it at will.
The usage of objects can be seen quite a bit everywhere. Let's take website users to see a real-life example of how PHP objects can be utilized. A website could define a user as an object with its own unique properties, such as its name, profile picture, and more that can change depending on the input of the user. It allows the website to recycle the same code and tweak it as needed, preventing mass amounts of data from accumulating over time. Objects keep the code simple and neat, allowing for multiple users to be easily created and also to allow users to be able to all be able to perform the same tasks, such as logging in.
All of this ties into how PHP is a server-side language, allowing for various inputs to come in from the server and change the output accordingly.
Click here to start learning how to use objects in PHP!
You may be wondering, "What are classes? Why is this the first thing that I have to learn? I thought we were learning about objects?" Well, you are! Classes define the object.
Classes hold the various properties and various possible actions of an object. For example, within an object that is meant to describe a person you can include the variables $age and $name. You could also have a function in there that every person can do, such as say hello. In essence they are your object.
Basic class syntax looks like this:
class Nameofclass { }
Within the brackets you can create variables for later use, the public tag making it accessible to whoever wants it:
class Car { public $color; public $model; }
Once this is done you can make a new object, or an instance of an object:
$myCar = new Car();
Within the parentheses you can put in various properties:
$myCar = new Car("blue", "Civic");
But how do you apply the properties to the class? Let's find out!
Now that we've created an instance of an object, how is it actually created? For this we use the constructor method, a method being a function within a class. You use the constructor method with the following syntax:
public function __construct(){ }
Yes, you need the two underscores in front of the construct part of the function in order for it to work. Within the brackets you set various variables to the specified parameters, as in this example code:
class Car{ public $color public $model public function __construct($color, $model){ $this->color = $color; $this->model = $model; } } $myCar = new Car("blue", "Civic");
Let's figure out how this works. We set the properties of our car as a blue Civic. When it reaches the public function __construct() the program checks for the things within the parentheses of the construct function. In this case, the color and the model of the car, both previously declared variables within the class
Once the inside of the function, you use $this->yourproperty = $prop to assign the properties to the class for that one instance of the class. For example, the first $this is referring to the first property of the car, in this case the string "blue". It continues down the line like that, assigning the variables within the class a value based on the specified properties of the instance of the class.
Now you're probably wondering about other functions within the class. Click here to learn about methods!
Methods, or functions within a class, are pretty similar to what we just learned:
public function functionname(){ }
And this is how they're called:
$myCar -> method();
Within these functions you can cause various things to happen, much like a normal function. However, you have to return something to outside of the class in order for it to be utilizable outside of the class. Let's say you wanted to tell us about your car. You would have to code it like this:
class Car{ public $color; public $model; public function __construct($color, $model){ $this->color = $color; $this->model = $model; } public function talk(){ return "I have a " . $this->color . " " . $this->model . "!"; } } $myCar = new Car("blue", "Civic"); echo $myCar -> talk();
This would display "I have a blue Civic!"
Now that we've learned how to create an object in PHP and how to make it do some things, why don't we move on to some of the built-in functions of PHP objects?
Now that we've covered the basics, we can start moving onto some other stuff. There are three built-in functions that you need to know about for objects that are particularly useful in if statements:
is_a($variableInQuestion, "className")
This function checks if a variable is of a certain class.
property_exists($variableInQuestion, "propertyName")
This function checks if a variable has a certain property.
method_exists($variableInQuestion, "methodName")
This function checks to see if a variable of the specified method.
An example of each:
is_a($myCar, "Car")
property_exists($myCar, "color")
method_exists($myCar, "drive")
Now we can move on to inheritance!
With inheritance we can tie multiple classes to each other. For example, we might have three classes: vehicle, car, and truck. We want to be able to tie the properties of both a car and a truck back to vehicle, as they are both a vehicle, while still keeping car and truck separate. We could do that like this:
class Vehicle{ } class Car extends Vehicle{ } class Truck extends Vehicle{ }
With this we can transfer attributes of the parent class to the child classes. In the following example all of the classes have wheels, even though it's only specified in one of the classes:
class Vehicle{ $hasWheels = true; } class Car extends Vehicle{ } class Truck extends Vehicle{ }
However, the child classes aren't limited to the properties of the parent class; they can change them quite easily:
class Vehicle{ $hasWheels = true; } class Car extends Vehicle{ $hasWheels = 4; } class Truck extends Vehicle{ }
The parent classes can also have the ultimate decision on everything, though; you can make methods the same throughout a parent class and its child classes, even if other functions are specified in the child classes:
class Vehicle{ final public function honk() { return "HONK HONK"; } } class Bicycle extends Vehicle{ final public function honk() { return "BEEP BEEP"; } }
If told to echo the honk() function, Bicycle will be unable to echo "BEEP BEEP" due to the final public function that Vehicle has set up.
Now that we've learned about inheritance, why don't we move on to constants?
At some point in your coding you may want a certain variable to stay the same throughout. This is where the const keyword comes in handy. When placed before a variable (in the format const var = 0; no $) in keeps the variable the same in every instance of the object. These are only on a class-by-class basis, though.
You can access these constants at any time in your code, even if there aren't any instances of the class that have ben created yet! This is found when you access the class' scope, which is what contains the class' variables. To do so you use the following syntax:
Class::const
The double colon is called the scope resolution operator, which finds the constant that you are looking for. An example of this being put to use would be checking if my car is blue in an if statement:
if (Car::blue) { }
The same can also be done to methods within a class, albeit with a static keyword instead. The same can also be done to properties, still checking with the scope resolution operator:
public static function honk() { } public static $hasWheels = true;
The function and the property can now be accessed at any point outside of the class. The function can be performed and the property checked.
And thus concludes the objects section of the PHP tutorial. Click here to get back to the homepage of the PHP tutorial.