Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101447 Viewers
61 Video lessons · 88208 Viewers
71 Video lessons · 72089 Viewers
56 Video lessons · 103848 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.