PHP classes and objects
PHP is an object-oriented language. An object-oriented language is a language where data types of data structures are defined by the programmer as well as their properties and the things that can be done with them.
This tutorial focuses on:
- What is a class?
- Creating a class
- Creating variables for a class
- Creating functions for a class
- Instantiating a class
- Creating a class constructor
- Referencing the variables of a class with an object
- Using the functions of a class with an object
What is a class?
A class is a template for a real world object to be used in a program. For example, a programmer can create a car class which emulates a car. This class can contain the properties of a car (color, model, year, etc.) and methods (functions) that specify what the car does (drive, reverse, stop, etc.).
Creating a class
To create a class, use the class keyword, followed by the class name, followed by an opening brace and a closing brace. All the specifics and logisitcs of the class go between the braces.
Creating variables for a class
Class variables are known as properties, because they store important information in regards to the class - hence, they are the classes properties. We will be referring to class variables as 'variables' or 'properties' interchangeably throughout this lesson.
The above code uses the Book class and creates two variables for it. The first is $title - a variable which will store the title of the book. The second is $numPages - a variable which will store the number of pages in the book.
NOTE: When declaring class variables, they must be declared with the var keyword. Variables that are not declared as class variables are not declared with the var keyword.
Creating functions for a class
Class functions are just like regular functions, except they are defined inside a class definition. Class functions are known as methods, and they can be used to either retrieve data or manipulate data. We will be referring to class functions as 'functions' or 'methods' interchangeably throughout this lesson.
An explanation of each function from the above example:
- setNumPages($numOfPages) - Used to set the number of pages in the book, this is done through its parameter $numOfPages. The function sets the class variable $numPages to whatever value you supply to it through its parameter $numOfPages.
- getNumPages() - Used to return the number of pages in the book, this is done through the use of the keyword return followed by the variable $numPages - the class variable that stores the value of how many pages there are in the book.
- setTitle($Title) - Used to set the title of the book, this is done through its parameter $Title. The function sets the class variable $title to whatever value you supply to it through its parameter $Title. The variables $Title and $title although spelled the same way, are not the same because their capitalization is different - remember that variable names are case sensitive.
- getTitle() - Used to return the title of the book, this is done through the use of the keyword return followed by the variable $title - the class variable that stores the title of the book.
Instantiating a class
So you have created your class with its variables and functions, and now what to do with it? Now you can create objects based on how you designed your class. The process of creating an object is called instantiation. You can create an object by creating a variable which stores the object, and using the new keyword and the name of the class to instantiate from.
Creating a class constructor
What if you wanted an object to automatically have a certain value when it is instantiated? For example, if you wanted all instances of the Book class to automatically have 200 pages? This is what class costructors are for. Class constructors are used to set objects to have certain values when they are initialized. The function used for this task is the __construct() function, and it goes inside the class definition.
Did you notice a new keyword? The keyword this is a special keyword used when creating classes. The purpose of it is to refer to the current class. The code inside the __construct function from the example above could have been $Book->$numPages = 200; instead of $this->$numPages = 200;, and the result would have been the same. When creating classes, $this can be used synonomously with the name of the class (in this case the class being the Book class).
In the above example, the __construct() function specifies that all instances of the Book class will automatically have 200 pages.
Referencing the variables of a class with an object
You can reference the variables of a class with an object by referring to them by name.
This example would reference the $numPages variable of the $yellowBook class. Notice how in the reference to the variable there is no $ symbol in front of the variable name. When referencing a class variable with the object name, you do NOT have to use the $ symbol.
Using the functions of a class with an object
Just as with the variables of a class, you can use the functions of a class with an object by referring to them by name.
This example would call the getNumPages() function of the Book class.