Member-only story
PHP Classes and Objects
A class can be thought of as a set of blueprints that describe something. From this set of blueprints, individual examples can be made. These examples, often called instances, are single objects. If we take pets as an example, we can use a class to describe pets in general. The class will tell us that the pet should have a name, an age, and a species, among other pieces of information. It is only when making an instance of a class that we get more specific details about these pieces of information. By creating an instance, we go from knowing to expect a pet to have a name, age, and species to knowing that we are talking about a 7 year old dog, named Baby.
To create a class, start with the following format. Although the name of a class does not need to be capitalized, it is conventional to do so.
class Name{
//class information
}
A class is assigned characteristics using attributes. An attribute can be any piece of data that describes the object that the class represents. For example, a pet class could be written as follows.
class Pet{
var $name;
var $age;
var $species;
}
For the pet class, name, age, and species are attributes of the class. The “var” keyword before the attributes indicates that these properties are attributes of the class. There are four different keywords that are used to…