Join David Powers for an in-depth discussion in this video A quick primer on using PHP objects, part of Accessing Databases with Object-Oriented PHP.
- View Offline
If you haven't worked with PHP Objects before, this quick primer will provide you with sufficient knowledge to follow the instructions in this course. So what are objects? An object is a data type that can store and manipulate values. You create an object from a class, which defines what an object can do. A class is basically a collection of related functions and values packaged as a single unit. We'll be working with PHP built in classes, so there's no need to create the class definitions yourself.
It's all done for you by PHP. Functions defined by a class are referred to as the object's methods. Values stored by an object are referred to as the object's properties. A common example that's used to describe objects is a car, so we'll use that here. An important property of any car is its color. For example you might have a red car, or it could be blue. Other important properties are the make of the car and the number of miles on the clock. All cars share similar properties, but each car is unique.
Similarly, each object that you create from a PHP class is unique. Methods dictate what an object can do. In the case of a car, accelerate is a useful method. Although all cars can accelerate, they operate independently of each other. In programming terms, you create an instance of a class, or instantiate an object. This simply involves using the class's constructor method with the new operator and storing the object in a variable.
The constructor method has the same name as the class, and it often accepts one or more arguments to initialize the object's properties. In our very simple example, we're passing red as the argument to the constructor. And we get a red car. You can create more than one object from the same class. So this creates another car, this time it's blue. You access an object's properties using the arrow operator, which consists of a hyphen and a greater than symbol.
Just append the arrow operators with a variable that contains the object, followed by the name of the property. The arrow operator is also used to call an object's methods. So if car objects have stop and accelerate methods, you can make car2 stop, while car2 accelerates away. So what does this got to do with database objects? Quite simply, the PDO and MySQLi classes that we'll be working with create objects that connect to the database.
The example files use only one connection object, but you could create multiple objects to connect to different databases. Each object is independent, so you could use them to transfer data from one database to another. You use the connection object's methods to interact with the database, such as to execute queries. Methods are basically functions, so they can take arguments. Class constants are frequently used as arguments. Constants are all in uppercase. PDO constants begin with PDO and a double colon, which is known as the scope resolution operator.
MySQLi constants begin with MySQLi underscore. Calling an object's method often returns a new object. This usually contains the results of a query or a prepared statement. For example, if you call the database connection object's query method, and pass it a select query, it returns an object containing the result set in both PDO and MySQLi. Notice that you don't need to use the new operator when an object is returned like this.
The new object will have methods of its own. Just remember to use the arrow operator to call the object's methods. Similarly, use the arrow operator to access an object's properties. And that's really all you need to know about PHP objects to follow the rest of this course.
- Connecting to a database with PDO or MySQLi
- Fetching a result set
- Executing simple non-SELECT queries
- Sanitizing user input
- Binding input and output values
- Passing an array of values to the execute() method
- Working with advanced PDO fetch methods
- Executing a MySQLi transaction
- Freeing resources that are no longer needed
- Submitting multiple queries
- Creating an instance of a class from a result set