Introducing Objects3:45 with Alena Holligan
We won't be building our own object, but we will be working with a PDO object, so I want to make sure you’re clear on some of the terminology and syntax involved in working with objects. An object is a complex variable type that bundles other variables and functions together into a single unit. Those variables are called "properties" of the object and those functions are called "methods" of the object. We’ll be utilizing a PHP Data Object (PDO) to connect to our database.
In our real world example of this, say the object is a dog. A dog has attributes like gender, weight, and hair color. It also has Actions such as Eating, Sleeping, and Walking. If a dog is the object, then Gender, Weight and Hair Color are Properties and Eating, Sleeping, and Walking are Methods.
Using the PHP Mailer Object:
$mail = new PHPMailer(); $mail->from = "email@example.com"; $mail->to = "firstname.lastname@example.org"; $mail->message = "This is the email body"; $mail->send();
Links to more Resources
I've already created a SQLite database with the items from the array. 0:00 I've also added a few more items. 0:05 Let's walk through how to retrieve that data with PHP. 0:07 We can then use that data on our website. 0:10 Since version 5.1, PHP comes with a built in mechanism for connecting to a database. 0:14 A PHP Data Object or PDO for short. 0:19 PDO provides some great methods for 0:23 working with all kinds of databases including SQLite and MySQL. 0:25 We'll be working with a PDO object. 0:30 So I want to make sure you're clear on some of the terminology and 0:32 syntax involved in working with objects. 0:35 An object is a complex variable type. 0:38 It bundles together variables and functions into a single unit. 0:41 Those variables are called properties, and those functions are called methods. 0:45 To give you a real world example of this, see the object is a dog 0:50 [SOUND] a dog has attributes like gender, weight, and hair color. 0:55 It also has actions such as eat, sleep and walk. 1:00 If a dog is the object, gender, weight and hair color are properties. 1:06 And eating, sleeping and walking are methods. 1:12 We're already using an object in this project, 1:17 the third party library used to send an email from our suggestion form. 1:20 It provides a PHPMailer object. 1:24 PHPMailer objects have properties to store information like subject and 1:27 a form address. 1:32 They also have methods which do things like Add attachment, and Send. 1:33 To access the objects, properties, and methods, we use a hyphen and 1:39 a greater than sign. 1:43 Two characters that together, look like a single arrow. 1:45 [SOUND] Let's say that we have a PHPMailer object called, Mail. 1:47 We can access the properties in that object by specifying the object name. 1:52 Mail, the single arrow, and then the property name, like subject. 1:57 We can call a method on that same object, by specifying the object's name, mail, 2:02 the single arrow, and the method name, like send, followed by two parentheses. 2:07 Methods are like functions. 2:13 They can receive arguments inside those parentheses. 2:15 This send method receives no arguments. 2:19 But you still need the parentheses. 2:21 The Add attachment method, on the other hand, does receive an argument. 2:23 It receives a reference to the file that you want to attach. 2:27 The next thing we need to discuss is the idea of a class. 2:31 In PHP, a class is a type of object or an object definition. 2:35 It's like a blueprint or a recipe for an object. 2:40 The class defines what properties and 2:43 methods will exist inside any objects of that class. 2:45 Each object is said to instantiate its class. 2:50 Which means it is an instance or a separate object made from that class. 2:54 PHPMailer is actually a class. 2:59 All PHPMailer objects have certain properties and 3:02 methods because they instantiate that PHPMailer class 3:06 which defines what those properties and methods will be. 3:10 PDO is also a class. 3:14 It's built into PHP. 3:16 So we don't have to include a third party library to use it. 3:18 We can create an object that instantiate the PDO.class. 3:21 And that object will have the properties and methods defined by that class. 3:25 We can then use that object to query the database. 3:31 Does all that make sense? 3:35 If you're still unsure, you can check the Teacher's Notes for more information. 3:36 But don't worry too much. 3:40 We'll be taking a closer look at the PDO class next. 3:41
You need to sign up for Treehouse in order to download course files.Sign up