Enter your Sign on user name and password.

Forgot password?
Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Use Chrome browser to play professor video
Matthew M.

Matthew M.

Object Methods

Slide Duration:

Table of Contents

I. Advanced PHP with MySQL
Course Introduction

13m 36s

Intro
0:00
Advanced PHP w/ MySQL
0:13
Course Introduction
0:14
Course Content
0:59
Web Application: Educator Store
1:45
Web Application: Educator Store
1:46
Object-Oriented Programming
6:09
Object-Oriented Programming Overview
6:10
MySQL
6:50
MySQL Overview
6:51
Example: Command Prompt & MySQL
8:34
What You Will Learn
9:34
What You Will Learn
9:35
Course Prerequisites
11:52
Course Prerequisites
11:53
Advanced Course Development Environment

18m 46s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Development Environment Setup
1:36
Firefox Web Browser, XAMPP, and PSPad Text Editor
1:37
Course Directory Structure
4:36
Course Directory Structure
4:37
phpDocumentor
7:23
phpDocumentor Overview
7:24
Parsing Source Code
7:44
docs Subdirectory
9:06
Asvanced PHP Course Documentation Example
10:21
Previous Educator PHP Students
14:14
Lib Directory Changes
14:38
Images Directory Changes
14:57
VERSION Constant
15:40
Comments Noting a Specific Version Number
17:04
Homework Challenge
17:30
Homework Challenge
17:31
Introduction to File I/O

35m 37s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
File I/O
1:34
File I/O Overview
1:35
Performing file I/O: 3 Steps
1:52
File Permissions
2:28
Read Permission & Write Permission
2:29
fopen()
4:47
fopen()
4:48
Two Required Parameters
5:01
Successful fopen(): File Handle and Resource
5:21
FALSE and E_WARNING Error
6:00
Example
6:39
File Paths
7:10
File Paths
7:11
File Access Modes
9:18
File Access Modes
9:19
Coding Example: fopen()
10:31
Using Relative and Absolute Path & Non-Existent File
10:32
File Pointers
15:55
File Pointers
15:56
Example File
16:11
Opening a File with fopen() Using 'r' as the Access Mode
16:22
fgets()
17:55
fgets() Overview
17:56
Coding Example: fgets() - Reading a Line at a Time
18:26
feof()
20:10
feof() Overview
20:09
Coding Example: feof()
20:50
fclose()
23:43
fclose() Overview
23:44
Coding Example: fclose()
25:02
Coding Example: Current Stock
26:10
Coding Example: Current Stock
26:11
trim ()
31:39
trim () Overview and Example
31:40
Homework Challenge
33:35
Homework Challenge
33:36
Web Application Development

36m 36s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
Version 1.0 Changelog
0:31
catalog.php
0:32
Function Library: fileLIB.php
5:00
Version 1.1 Changelog
27:12
createItemDataArray() and createDeptDataArray()
27:13
outputSimpleItemLink() and outputSimpleDeptLink()
32:06
HTTP & the POST Method

34m 36s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
HTTP Model
1:28
HTTP Model
1:29
Client-Server Model 2 Steps Process
2:00
HTTP Messages
2:23
HTTP Messages
2:24
HTTP Requests
3:09
HTTP Requests
3:10
Example: HTTP Request Header
3:17
Coding Example: HTTP Requests
4:29
HTTP Responses
7:15
HTTP Responses
7:16
Example: HTTP Response (Header & Body)
8:00
header()
11:47
header() Overview
11:48
Coding Example: header
14:02
Coding Example: header() Error
14:52
GET Method
18:55
GET Method
18:56
Coding Example: GET Method
20:13
POST Method
21:09
POST Method
21:10
Coding Example: Sample Form Using the POST Method
23:50
Accessing POST Data in PHP
26:38
Accessing POST Data in PHP
26:39
Coding Example: Accessing POST Data via $_POST
28:15
GET vs. POST
29:54
GET vs. POST
29:55
Example: Accessing POST Data via $_POST
31:20
Example: Accessing POST Data via $_POST
31:21
Homework Challenge
33:00
Homework Challenge
33:01
Web Application Development

14m 11s

Intro
0:00
Version 2.0 Changelog
0:09
Updating the Site's Current Form to Use the POST Method
0:10
Creating Admin Website
2:55
Dynamically Generated Department List
12:40
Writing to Files

17m 38s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Review of File I/O
0:40
Review of File I/O
0:41
fwrite()
1:38
fwrite()
1:39
fputs()
2:20
'r+'
2:31
Coding Example: fwrite()
2:59
fopen() Access Modes
8:41
'a' and 'a+'
8:42
'w' and 'w+'
11:34
Homework Challenge
14:47
Homework Challenge
14:48
Web Application Development

28m 14s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
Updated Open File Functions
0:19
openItemsDataFile ( ), openDeptsDataFile ( ), and openLastAddedFile ( )
0:20
insertItem()
6:25
insertItem() Overview
6:26
Functions: createItemDataString ( ) and updateLastItemAdded ( )
7:07
addItemToDepartment ()
16:03
addItemToDepartment () Overview
16:04
Functions: createDeptDataString ( ) and updateDepartment ( )
17:55
HTTP POST: File Uploads

22m 51s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
POST Data Encoding
1:13
POST Data Encoding
1:14
When Data is Included in the Body of the HTTP POST Request and is of the Content Type
1:31
When Uploading Files Using HTTP POST
3:17
Uploading Files
6:22
Uploading Files
6:23
If Encoding Type is Not Set to Multipart/Formdata
9:27
$_FILES Superglobal
10:59
$_FILES Superglobal
11:00
Structure of $_FILES
12:15
Structure of $_FILES: Name, Type, tmp_name, Error, and Size
12:16
Coding Example: $_FILES Superglobal
13:34
Moving an Uploaded File
15:39
Introduction to Moving an Uploaded File
15:40
move_uploaded_file ( ): Definition and Example
16:40
Homework Challenge
21:19
Homework Challenge
21:20
Web Application Development

15m 30s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Uploading Item Image File
1:22
Uploading Item Image File
1:23
Uploading Item Image File (cont.)
2:43
Altering insertItem ( )
2:44
Helpfer Functions: getFileExtension ( ) and moveUploadedFile ( )
4:30
isValidItem ( )
13:03
Introduction to Object-Oriented Programming

32m 44s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Object-Oriented Programming
0:56
Introduction to Object-Oriented Programming
0:57
Associative Arrays
5:27
Associative Arrays
5:28
Classes
7:27
Classes Overview
7:28
Defining Classes
8:24
Defining Classes
8:25
Declaring Class Properties
9:25
Coding Example: Classes
10:11
Objects
12:18
Objects Overview
12:19
Classes vs. Objects
12:49
Classes vs. Objects
12:50
Instantiating Classes
14:58
Instantiating Classes
14:59
Coding Example: Instantiate an Item Object
16:30
Object Properties
19:21
Access and Set an Object's Property
19:22
Coding Example: Set & Access the Properties of an Item Object
24:23
Homework Challenge
30:22
Homework Challenge
30:23
Web Application Development

17m 47s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 5.0 Overview
0:50
Version 5.0 Overview and Examples
0:51
Outputting Object Properties
12:38
Outputting Object Properties
12:39
Array Object Properties
14:18
Access and Set Array Value
14:19
Object Methods

39m 57s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Object Methods
0:52
Definition of Object Methods
0:53
Calling Object Methods
3:25
Calling Object Methods
3:26
Coding Example: Simple Object Methods
4:44
Object Methods and Return Values
7:02
$this Variable
10:06
$this Variable: Definition and Example
10:07
$this Variable (cont.)
15:51
$this Variable (cont.)
15:52
Getters & Setters
21:21
Getters & Setters Methods
21:22
Object Methods In Strings
25:46
Object Methods In Strings
25:47
Coding Example: Outputting Method Return Values in Strings
27:41
Using $this to Call Other Methods
28:49
Using $this to Call Other Methods
28:50
Homework Challenge
34:18
Homework Challenge: 1-3
34:19
Homework Challenge (cont.)
36:20
Homework Challenge: 4-6
36:21
Homework Challenge (cont.)
37:52
Homework Challenge: 7-10
37:53
Web Application Development

17m 30s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 6.0 Overview
0:40
Version 6.0 Changes & Examples
0:41
Item Methods
0:53
Item Class Definition: getImageFilename()
0:54
Coding Example: getImageFilename()
1:58
Department Methods
7:33
addItem(), removeItem(), and isItemInDept() Method
7:34
addItemtToDepartment() & buildDeptObject() Functions
11:46
A Default Value Needed to Be Set for $items
16:26
Object Constructors

22m 20s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Object Constructors
0:32
Introduction to Object Constructors
0:33
Coding Example: Object Constructors
1:43
Defining Constructors
3:26
Defining Constructors
3:27
Coding Example: Constructors with No Arguments
4:24
Constructor Arguments
8:09
Constructor Arguments
8:10
Coding Example: Constructor Arguments
9:49
Important Notes
16:13
Important Notes
16:14
Homework Challenge
17:35
Homework Challenge: 1 - 4
17:36
Homework Challenge (cont.)
19:13
Homework Challenge: 5 - 9
19:14
Homework Challenge (cont.)
21:40
Homework Challenge: 10 and 11
21:41
Web Application Development

16m 19s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 7.0 Overview
0:39
Version 7.0 Overview
0:40
Item Constructor
2:19
Item Constructor
2:20
Department Constructor
7:47
Department Constructor
7:48
Customer Constructor
12:23
Customer Constructor
12:24
More Magic Methods

53m 37s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
Magic Methods
1:12
Magic Methods
1:13
Destructors
2:45
Destructors Overview
2:46
Coding Example: Calling Destructors
4:30
Coding Example: Object Destructor
9:19
_to String ()
16:12
_to String () Overview
16:13
Coding Example: _to String () Magic Method
18:10
Access Modifiers
21:23
Introduction to Access Modifiers
21:24
Access Modifiers: Public
21:39
Access Modifiers: Private
22:18
Access Modifiers: Protected
22:41
Object Properties and Methods
23:06
Coding Example: Public Access Modifiers
26:48
Coding Example: Private Access Modifiers
28:30
_get()
31:37
_get() Overview
31:38
Coding Example: _get () Magic Method
33:30
_set ()
36:23
_set () & the Magic Method
36:24
Using Getters & Setters
44:37
Coding Example: Using Getters & Setters
44:38
Homework Challenge
50:33
Homework Challenge: 1 - 6
50:34
Homework Challenge (cont.)
51:41
Homework Challenge: 7 - 12
51:42
Web Application Development

31m 12s

Intro
0:00
Version 8.0 Review
0:12
Version 8.0 Review
0:13
private Object Properties
1:15
private Object Properties
1:16
Coding Example
2:54
_toString() Methods
6:51
_toString() Methods
6:52
Coding Example
8:09
DataFile Class
13:27
DataFile Class & I/O Operations
13:28
Using DataFile Class: Instantiate a DataFile, DataFile open () Method, and DataFile close () Method
18:09
Homework Challenge
29:35
Homework Challenge
29:36
Classes vs. Objects

37m 49s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Class vs. Object Review
0:58
Class vs. Object Review
0:59
Static Properties
2:42
Static Properties
2:43
Scope Resolution Operator (::)
5:26
Scope Resolution Operator (::) & Static Property
5:27
Coding Example: Accessing a Static Property Using the Scope Resolution Operator
7:57
Coding Example: Accessing a Static Property in a Class Definition
10:33
Coding Example: Using a Static Property as an Instance Counter
14:18
Static Methods
18:51
Static Methods Overview
18:52
Coding Example: Static Methods
21:39
Classes vs. Objects
26:28
Classes vs. Objects: Diagrams
26:29
Class Constants
30:24
Class Constants Definition
30:25
Coding Example: Using Class Constants
31:55
Homework Challenge
34:01
Homework Challenge: 1 - 5
34:02
Homework Challenge (cont.)
35:25
Homework Challenge: 6 - 10
35:26
Web Application Development

21m 7s

Intro
0:00
Version 9.0 Overview
0:12
Version 9.0 Changes & Examples
0:13
Output Class
1:08
Output Class & Static Methods
1:09
outputHtmlSelector ( ) Method
2:43
outputItemSelector ( ) Method
4:53
outputDeptSelector ( ) Method
6:42
buildObject() Methods
11:54
buildObject() Methods & Examples
11:55
Exceptions

22m 43s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
try Blocks
0:44
try Blocks Definition
0:45
catch Blocks
2:03
catch Blocks: Definition and Syntax
2:04
Coding Example: try/catch Blocks
3:48
When an Exception Object is Caught & Error Handling
5:59
Exception Class
7:02
Exception Class
7:03
Coding Example: Using try/catch to Catch an Exception Thrown from a Method
8:04
Re-Throwing Exceptions
16:39
Re-Throwing Exceptions
16:40
Coding Example: Re-throwing an Exception
17:21
Homework Challenge
20:40
Homework Challenge: 1 - 5
20:41
Homework Challenge (cont.)
22:17
Homework Challenge: 6
22:18
Web Application Development

22m

Intro
0:00
Version 10.0 Overview
0:11
Updating addItem.php, addItemToDept.php, and error.php
0:12
Updating DataFile Class: open ( ) Method & try/catch blocks
7:45
Version 10.1 Overview
13:37
Version 10.1 Changes & Examples
13:38
Updating DataFile Class: close ( ) Method & logWarning ( )
20:17
Cookies

39m 47s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
What is a Cookie?
1:15
Definition of Cookie
1:16
HTTP - A Stateless Protocol
2:17
HTTP - A Stateless Protocol
2:18
Purpose of Cookies
4:10
Set - Cookie Header
6:21
Introduction to Set - Cookie Header
6:22
Coding Example: Set - Cookie Header
7:38
Cookie Header
9:45
Introduction to Cookie Header
9:46
Example: Cookie Header
10:27
setcookie()
12:33
Introduction to setcookie()
12:34
Example: Using setcookie
13:54
Cookies Attributes
15:48
Cookies Attributes Overview
15:49
Common Cookies Attributes: Domain
16:24
Common Cookies Attributes: Path
17:04
Common Cookies Attributes: Expires
19:34
Coding Example: Setting Cookie Attributes
20:58
$_COOKIE Superglobal
27:03
$_COOKIE Superglobal
27:04
Coding Example: Accessing Cookies from PHP Using $_COOKIE
29:45
Security Implications
34:44
Security Implications
34:45
Homework Challenge
36:10
Homework Challenge: 1 - 4
36:11
Homework Challenge (cont.)
37:47
Homework Challenge: 5 - 7
37:48
Web Application Development

39m 11s

Intro
0:00
Lesson Overview
0:17
Lesson Overview
0:18
instanceof Operator
1:34
instanceof Operator
1:35
Objects with Object Properties
2:53
Objects with Object Properties
2:54
Version 11.0 Overview
6:41
Version 11.0 Changes: Cart and CartItem Classes
6:42
Version 11.0 Coding Example
10:40
Version 11.1 Overview
31:03
Version 11.1 Changes & Examples
31:04
Introduction to Sessions

46m 8s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
What is a Session?
0:53
What is a Session?
0:54
Client-Side Sessions
3:51
Client-Side Sessions
3:52
Server-Side Sessions
5:04
Server-Side Sessions
5:05
Session IDs
6:26
Session IDs
6:27
The SID is Used by the Server to:
7:08
A Client Passes an SID to the Server with Each HTTP Request Via:
8:15
Sessions in PHP
12:19
Sessions in PHP
12:20
session_start()
14:40
session_start() Overview
14:41
Coding Example: Using session_start () to Continue a Session
18:03
$_SESSION Superglobal
22:06
$_SESSION Superglobal Overview
22:07
Coding Example: Setting a Session Variable via $_SESSION
25:22
Accessing Session Data
28:08
Accessing Session Data
28:09
Deleting Session Data
31:43
Deleting Session Data
31:44
Coding Example: Deleting Session Data
32:16
Configuring PHP Sessions
36:14
session.cookie_lifetime, session.cookie_domain, and session.cookie_path
36:15
Coding Example: Configuring PHP Sessions
37:28
Configuring PHP Sessions (cont.)
40:56
session.use_cookies, session.use_trans_sid, and session.use_only_cookies
40:57
Coding Example: Configuring PHP Sessions
42:25
Homework Challenge
42:55
Homework Challenge: 1 - 5
42:56
Homework Challenge (cont.)
44:10
Homework Challenge: 6 - 10
44:11
Homework Challenge (cont.)
45:27
Homework Challenge: 11 - 13
45:28
Web Application Development

11m

Intro
0:00
Version 12.0 Overview
0:12
Version 12.0 Changes Part I: Updating 'viewCart.php' & 'checkout.php'
0:13
Version 12.0 Changes Part II
5:03
Destroying Sessions

29m 59s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Destroying Sessions
1:02
Destroying Sessions
1:03
session_destroy()
2:10
session_destroy() Overview
2:11
Coding Example: Setting a Session Variable and Destroying a Session
3:18
Deleting Session Cookies
8:38
Deleting Session Cookies
8:39
Coding example: Deleting Session Cookies
9:17
Review of Steps
21:07
Review of Steps
21:08
Garbage Collection
21:50
Garbage Collection Overview
21:51
Coding Example: Garbage Collection
24:30
Homework Challenge
26:28
Homework Challenge: 1 - 4
26:29
Homework Challenge (cont.)
28:16
Homework Challenge: 5 - 9
28:17
Web Application Development

19m 14s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
_autoload()
0:46
Introduction to _autoload ()
0:47
Version 13.0 Overview
4:06
_autoload () and SessionManager Class
4:07
Updating 'viewCart.php' & 'checkout.php'
11:16
Introduction to Databases

21m 24s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
Flat Files
1:09
Flat Files: Definition and Example
1:10
Problems Associated with Using Flat Files as a Database
2:15
Relational Databases
3:29
Relational Databases
3:30
Relational Database Management System (RDBMS)
3:50
Tables
7:43
Tables
7:44
Columns
9:24
Columns
9:25
What is SQL?
10:45
Introduction to Structured Query Language
10:46
Standard Data Types of SQL
12:23
Primary Keys
13:19
Primary Keys
13:20
Primary Key Examples
16:36
Primary Key Examples
16:37
Introduction to MySQL

28m 11s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
What is MySQL?
1:33
MySQL
1:34
MySQL Monitor
5:16
MySQL Monitor Overview
5:17
XAMPP & MySQL
5:58
In the MySQL Command: hostname, username and -p
7:05
Connecting to a MySQl Server
9:52
Connecting to a MySQl Server
9:53
Using MySQl Monitor
13:48
Using MySQl Monitor
13:49
GRANT Command
15:10
GRANT SQL Command
15:11
Privileges & Objects
18:18
Username, Hostname, and Password
19:37
Specifying Objects
20:05
Creating a PHP User Account
21:41
Creating a PHP User Account Overview
21:42
Meaning of all & *.*
23:35
Example: Creating a PHP User Account
23:48
Required Homework
26:27
Required Homework: 1 - 5
26:28
Required Homework (cont.)
27:28
Required Homework: 6
27:29
Creating Databases & Tables

23m 1s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
Creating a Database
1:40
Introduction to Creating a Database
1:41
Example: Creating a Database
3:33
CREATE TABLE Command
6:43
CREATE TABLE Command
6:44
Data Types
7:39
Numeric Data Types: INT, INT UNSIGNED, and DECIMAL
7:40
String Data Types: CHAR (M), VARCHAR (M), and TEXT
9:23
Colum Attributes
11:00
PRIMARY KEY Attribute
11:01
AUTO INCREMENT Attribute
11:35
Items Table
13:07
Items Table
13:08
Useful Commands
17:04
SHOW TABLES Command & DESCRIBE Utility Statement
17:05
Example: Creating an Items Table
17:58
Required Homework
20:51
Required Homework: 1 - 6
20:52
Required Homework (cont.)
21:55
Required Homework: 7 - 9
21:56
SQL Command: INSERT

27m 11s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
INSERT Command
1:20
SQL INSERT Command
1:21
Specifying Values
3:16
Columns with Numeric Data Types
3:17
Columns with String Data Types
3:34
Columns with AUTO INCREMENT
4:07
Inserting Items
5:21
Inserting Items
5:22
Example: Inserting Items
7:03
SQL Text Files
14:04
SQL Monitor Client
14:05
MySQL Monitor & the < Operator
15:15
Example
17:35
Required Homework
22:32
Required Homework: 1 - 6
22:33
Required Homework (cont.)
24:19
Required Homework: 7 - 10
24:20
SQL Command: SELECT

24m 57s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
SELECT Command
0:42
SELECT Command: Definition and Syntax
0:43
Results of a SELECT Query
1:53
To Specify the Retrieval of All Columns from a Table
3:06
Example: SELECT Command
4:18
WHERE Clause
7:11
WHERE Clause
7:12
A SELECT Query with WHERE Clause has the Following Syntax
8:10
A Where Condition can Contain both Logical and Comparison Operators
9:44
Literal Values
13:24
Literal Values
13:25
Example: Literal Values
14:18
ORDER BY Clause
15:31
ORDER BY Clause & SELECT Query
15:32
Ascending and Descending Order
17:30
An ORDER BY Clause can Specify Multiple Columns to Order the Rows by
18:01
Example: SELECT Query with WHERE and ORDER BY Clause
20:32
Homework Challenge
21:59
Homework Challenge
22:00
Homework Challenge (cont.)
23:54
Homework Challenge
23:55
Using PHP from MySQL

32m 43s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
PHP/MySQL Architecture
0:56
PHP/MySQL Architecture
0:57
MySQLi Extension
3:05
MySQLi Extension
3:06
Steps to Using MySQL from PHP
5:53
Four Steps to Using MySQL from PHP
5:54
Step # 3
7:18
Connecting to a Database
8:12
To Connect to a MySQL Database
8:13
To Test for Connection Errors
10:20
Running an SQL Statement
11:54
Running an SQL Statement
11:55
Example
13:30
Processing the Results
15:58
For non-SELECT Queries
15:59
MySQLi Object Property: affected_rows
16:19
For INSERT Statements on Tables with an AUTO_INCREMENT Column
18:07
Closing the Connection
19:45
Closing the Connection
19:46
Coding Example
21:24
Coding Example: Using MySQL from PHP
21:25
Homework Challenge
29:00
Homework Challenge: 1 - 4
29:01
Homework Challenge (cont.)
31:10
Homework Challenge: 5 - 9
31:11
Web Application Development

30m 30s

Intro
0:00
Lesson Overview
0:14
Lesson Overview
0:15
sprintf()
1:28
sprintf() Overview
1:29
Example
1:56
INSERT Statements with sprintf()
5:15
INSERT Statements with sprintf()
5:16
Version 14.0 Overview
10:00
Version 14.0 Changes & Examples Part 1
10:01
Version 14.0 Changes & Examples Part 2
21:58
SELECT QUERIES from PHP

24m 22s

Intro
0:00
Lesson Overview
0:14
Lesson Overview
0:15
SELECT Queries
1:21
SELECT Queries
1:22
MySQLi_Result Class
3:17
MySQLi_Result Class: num_rows
3:18
MySQLi_Result Class: fetch_assoc ( ) and fetch_object ( )
3:59
fetch_assoc()
5:17
fetch_assoc() Overview
5:18
Coding Example: Using fetch_assoc()
9:00
stdClass Objects
12:46
stdClass Objects
12:47
fetch_object()
14:19
fetch_object() Overview
14:20
Coding Example: Using fetch_object()
16:45
Freeing Result Set Memory
18:42
Freeing Result Set Memory
18:43
Coding Example: Using free ( )
20:01
Homework Challenge
22:07
Homework Challenge: 1 - 5
22:08
Web Application Development

17m 6s

Intro
0:00
Version 15.0 Overview
0:13
Version 15.0 Changes & Examples Part 1
0:14
Version 15.0 Changes & Examples Part 2
8:43
Linking Tables

24m 32s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
A Departments Table
1:08
Departments Table
1:09
How Could We Model This in a Table: Option 1
1:26
How Could We Model This in a Table: Option 2
3:12
Database Normalization
4:37
Database Normalization
4:38
Foreign Keys
8:47
Foreign Keys
8:48
Linking Table
11:26
Linking Table
11:27
Multiple Column Primary Keys
15:30
Multiple Column Primary Keys
15:31
Example
18:51
Required Homework
22:34
Required Homework: 1 - 3
22:35
Required Homework (cont.)
24:10
Required Homework: 4
24:11
Web Application Development

24m

Intro
0:00
Version 16.0 Overview
0:11
Version 16.0 Changes
0:12
Version 16.0 Coding Examples
4:12
Version 16.0 (cont.)
18:30
Version 16.0 Changes & Examples Part 2
18:31
SQL Command: DELETE

10m 33s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
DELETE Statement
0:30
DELETE Statement & Its WHERE Clause
0:31
Delete All Rows From a Table
2:57
Using DELETE from PHP
4:04
Using DELETE from PHP Overview
4:05
Coding Example: Using DELETE from PHP
6:37
Homework Challenge
8:53
Homework Challenge: 1 - 4
8:54
Web Application Development

16m 2s

Intro
0:00
Version 17.0 Overview
0:11
Version 17.0 Changes
0:12
Version 17.0 Coding Example
2:51
Transactions
12:50
Database Transaction
12:51
Steps in Using Transactions
14:00
SQL Command: UPDATE

32m 14s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
UPDATE Statement
0:54
UPDATE Statement & Its WHERE Clause
0:55
Example: UPDATE Statement
3:32
Using UPDATE from PHP
5:21
Using UPDATE from PHP Overview
5:22
Coding Example: Using UPDATE from PHP
6:53
htmlspecialchars()
9:04
htmlspecialchars()
9:05
Encoding Special Characters
9:23
Coding Example: htmlspecialchars()
13:41
addslashes()
20:40
addslashes()
20:41
Coding Example: addslashes()
22:52
Homework Challenge
27:55
Homework Challenge: 1 - 2
27:56
Homework Challenge (cont.)
30:14
Homework Challenge: 3 - 5
30:15
Homework Challenge (cont.)
30:41
Homework Challenge: 6 - 9
30:42
Web Application Development

12m 33s

Intro
0:00
Version 18.0 Overview
0:12
Version 18.0 Changes
0:13
Version 18.0 Coding Example
1:24
SQL: Joins

42m 16s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
DATETIME Data Type
1:52
DATETIME Data Type
1:53
Modeling Orders
5:12
Modeling Orders
5:13
Customers Table
10:46
Customers Table
10:47
Example
16:18
Example: Order Table in MySQL
16:19
Joins
22:26
Joins Overview
22:27
Inner Join
23:09
Example: Joins
24:11
Join Conditions
28:14
Join Conditions
28:15
Example
28:32
Join Queries
30:00
Example: Join Conditions in MySQL
30:36
Implicit Joins
34:42
Implicit Joins
34:43
Example: Implicit Joins
35:57
Aliases
37:28
Introduction to Aliases
37:29
Example: Aliases
38:55
Required Homework
40:32
Problem 1
40:33
Problem 2
41:01
Problem 3
41:17
Web Application Development

29m 34s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Inserting Orders
0:44
Inserting Orders
0:45
Version 19.0 Overview
3:45
Version 19.0 Changes & Example Part 1
3:46
Version 19.0 Changes & Example Part 2
13:18
Version 19.1 Overview
21:10
Version 19.1 Changes
21:11
Version 19.1 Coding Example
22:18
User Authentication

26m 13s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
User Authentication
1:31
User Authentication
1:32
Authentication with Sessions
2:34
Authentication with Sessions
2:35
Four Steps in Authentication via Sessions
2:50
Using Sessions to Restrict Access
3:58
Using Sessions to Restrict Access
3:59
Coding Example: Restricted Access Area
4:47
Authentication Methods
5:54
Authentication Methods Overview
5:55
Coding Example: Authentication Methods
7:31
Logging Out
9:57
Logging Out
9:58
Coding Example: Log Out
10:47
Users Table
13:50
Users Table
13:51
Example: Creating a Users Table
15:08
Password Hashing
17:30
Password Hashing
17:31
PHP and MySQL Built-in Hash Functions: sha1 ( ) and md5 ( )
18:43
Coding Example: Password Hashing
19:27
Required Homework
24:41
Required Homework: 1 - 4
24:42
Web Application Development

14m 36s

Intro
0:00
Version 20.0 Overview
0:13
Version 20.0 Changes & Examples Part 1
0:14
Version 20.0 Changes & Examples Part 2
5:05
Version 20.0 Changes & Examples Part 3
7:29
Version 20.0 (cont.)
8:31
Version 20.0 Changes & Examples Part 4
8:32
Loading...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Advanced PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Lecture Comments (1)

0 answers

Post by dave smith on August 15, 2015

In the homework solution, it would be better to add [] to the $header property like this: class HttpRequest{

public $uri;

public $headers = [];

rather than this: class HttpRequest{

public $uri;

public $headers;

That way you sure to not get an error even if you call the addHeader() method and pass in no arguments.

Object Methods

  • Classes can define functions, known as object methods, that instances of the class can execute.
  • An object method is defined within the curly braces of a class definition using the same syntax used when creating user-defined functions.
  • Object methods are called using the arrow operator ->. An object variable is followed by ->, the name of the method, and a set of parentheses, which can optionally include any arguments to be passed to the object method:
    $person1 = new Person();
    $person1->hello(); // object method requiring no arguments
    $person1->helloWithArg($arg1); // object method requiring arguments
  • Object methods can return values just like user-defined functions.
  • $this is a special variable in PHP that is used within a class’s methods to refer to the current instance of a class.
  • A getter method retrieves the value of an object’s property.
  • A setter method sets the value of an object’s property.
  • An object method’s return value can be output within double-quoted strings using curly brace syntax:
    $person1 = new Person();
    $person1->setFirstName(‘Joe’);
    echo “First: {$person1->getFirstName()}”;
  • Object methods can call other object methods of the same object instance by using the $this variable.

Object Methods

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:12
    • Lesson Overview
  • Object Methods 0:52
    • Definition of Object Methods
  • Calling Object Methods 3:25
    • Calling Object Methods
    • Coding Example: Simple Object Methods
    • Object Methods and Return Values
  • $this Variable 10:06
    • $this Variable: Definition and Example
  • $this Variable (cont.) 15:51
    • $this Variable (cont.)
  • Getters & Setters 21:21
    • Getters & Setters Methods
  • Object Methods In Strings 25:46
    • Object Methods In Strings
    • Coding Example: Outputting Method Return Values in Strings
  • Using $this to Call Other Methods 28:49
    • Using $this to Call Other Methods
  • Homework Challenge 34:18
    • Homework Challenge: 1-3
  • Homework Challenge (cont.) 36:20
    • Homework Challenge: 4-6
  • Homework Challenge (cont.) 37:52
    • Homework Challenge: 7-10

Transcription: Object Methods

Hello again, and welcome back to Educator.com's Advanced PHP with MySQL course.0000

In today's lesson, we are going to be expanding our knowledge of object-oriented programming by learning about object methods.0005

Specifically, we are going to talk about what object methods are and what their purposes are.0014

We are going to talk about the subject of how to call object methods--the syntax that is used to do that.0019

We are going to talk about a special variable, known as the this variable, which is going to be important for use within our object methods.0025

We are going to briefly talk about what are known as getter and setter methods.0033

And then, we are going to finish up by talking about how to output the results of object methods within double-quoted strings,0037

and how to use this this variable we are going to learn about to be able to call one object method from within another object method.0044

Classes can define functions.0055

In a way, we have been having our own function libraries.0058

For example, fileLIB.php has been a library that just contains a number of function definitions that we have been able to use.0060

Well, when you define a class (for example, the Item class), you can define functions.0067

And functions that are part of a class definition are known as methods.0075

And they are functions that work on a particular object.0081

So, when you define an instance of the Item class (for example), it might have a method that says returnCost.0087

And when you call that method on that particular item, it will return maybe the price property of that particular item.0095

The object methods are defined...and it is still a function, but again, the terminology with objects is to call them methods...0104

they are defined within the class definition, just like a class's properties.0112

And it is defined within the curly braces; we are going to take a look at what it looks like in a second.0116

It has the same syntax that we use for defining functions, in that it even uses the function keyword.0122

The one difference is that, at least for the time being, we are going to be placing the public keyword in front of the function keyword.0127

Let's take a look at a class Person where we have defined a class method.0137

For example, we have a Person class with two properties, firstName and lastName,0146

which would just be strings to represent the first name and last name of the person.0152

And this is what we have seen before; we have come across this when we first learned about objects, about how to define properties.0155

But also, if you look down here, you can see that we have defined a function.0161

It is within the curly braces of the class definition, which means that it is a method of this Person class.0165

And we use the keyword public, and then function; we say public function,0171

and then we define the method just as we would any other function that we have done before.0176

In this case, we define a method called hello that takes 0 arguments; it doesn't return anything; it just outputs an echo statement.0181

All the rules that apply to functions that we had learned about in the introductory course,0193

that you know about from your experience with PHP, are going to apply to object methods, as well.0198

We know that we can define methods and objects; but how do we call them?0206

Well, first of all, you have to have an instance of an object to call the method.0210

And then, in order to call the method, you use the same accessor syntax that we use to access the values of the different properties.0214

And you use that to call an object's method.0222

For example, let's say we define a variable called person1, and we set it equal to a new instance of this Person class that we have talked about.0226

Well, in order to call the hello method on that Person, we list the name of the variable, followed by the dash and the greater than sign.0238

And then, instead of following that by the property name, we follow it by the name of the function.0247

In this case, we had a function called hello.0251

And the way that you distinguish it as a function, as opposed to a property, is: you include a pair of parentheses afterwards,0253

which is sort of intuitive, because that is the syntax we use for functions.0262

Now, for example, if our hello method of our Person class took a parameter, then we would also specify the parameters0266

within the parentheses, just as you would any time we have been calling any other functions that take parameters.0277

Let's take a look at our Person class; we defined this function hello--let's take a look0285

at a file called hello.php that is going to call that method, so we can see this in action.0291

We have a test file here that includes the definition of the Person class.0297

We instantiate, or create a new instance of, a Person object.0302

We go ahead and use the accessor syntax to set the firstName and lastName properties of this particular Person object, this instance of a Person object.0307

And then, we are going to call, or invoke, the hello method for this particular Person object.0317

In this case, we use the dash, greater than sign, and then we say hello with parentheses.0324

If we just quickly look back, all our hello function does is outputs or echoes a statement that says 'hello.'0330

If we go and look at this lecture example, and we look at hello.php, we can see that the output of it is A person says 'Hello!'0338

And that is a result of calling this method on this Person object.0347

And so, that is the syntax that you use to invoke an object's method.0354

One other thing to do is (for example): we could, just as with other functions, include parameters.0362

So, let's include one parameter--it's a string called world.0368

Now, we could go to our Person class, modify the function definition so that it accepts a string, and then say,0375

"We are going to do the same thing; we are going to have an echo statement, but we are going to echo the string that was passed in."0386

So now, we have an object method that can take a parameter, as well.0398

So, if we go ahead and (both of these are saved) reload our page (and let's make this string that we are passing in...0403

OK, it is world, so it will be different), we can say, "A person says 'world.'0409

So there, you can see how an object method works just like a regular function does.0416

One other thing to note is that object methods can return values, just like a normal function can.0424

For example, let's say that we had an object that modeled a shopping cart (which is something we are actually going to be adding to our web application).0429

Let's say we already have that, and we have declared an instance of it.0435

So, we have this cart1 variable that is an object of our ShoppingCart class.0438

Maybe that ShoppingCart class has a method on it called calcCartTotal, which is a function we have been using in our utility library.0443

Well, if you run that calcCartTotal function on a particular cart object, let's say it calculates that cart total and returns it as a value.0453

So here, what you do in order to get the return value of the object method is: you call it just like you would a regular function.0459

And you can use an assignment statement; so in this case, we are assigning the value returned from the calcCartTotal function into the total variable.0469

And that is the same thing as if we had maybe, instead, done total= calcCartTotal, or maybe we passed it an array of the items in the cart.0481

Whereas this would return the value--the return value of this function takes its place, and total gets set to it;0503

the same thing happens here--the return value of this whole object statement gets returned and gets set into the value total.0509

For example, we could alter our hello method of our Person class,0520

so that instead of echoing the string, we could have it return a particular string.0531

For example, get rid of the echo statement and just have it return a static string that says, "A person says 'Hello!'"0536

So now, what we can do is: if we run this the way it is, what is going to happen is:0549

it is going to call this hello method; it is going to return a value; but we are not echoing it, and it is not echoing it in the function.0554

So, if we run this script again, nothing is going to happen; no output is going to happen for that function.0563

So, what we do is: we update it to just echo the result of that.0568

Here is an example of having an instance of an object, calling a method on it that returns a value,0574

and then using that value--using that return value, just as you would a return value from any other function.0582

Here, we are going to echo the value returned by this hello method of the Person object, which is going to be that string.0587

And if we refresh the page now, we can see that it goes ahead and outputs it.0595

That is an example of how object methods can also return values, as well.0600

Now, that particular function didn't do too much; it especially didn't do anything related to the object.0608

It just output a simple string: you could just create a regular function to do that.0615

Typically, the reason you create object methods is so that they can perform some actions on that particular object.0618

One of the main things that they can do is that the functions defined within an object can access the members,0625

or the properties, of that particular object--and actually even the other methods of a particular object.0632

And the way that you do that is: there is a special variable called this: it's $this, and you can use it within class methods.0637

And what it does is refers to a current instance of an object.0646

And it is kind of an abstract concept, so let's take a look at what it looks like in code, so that it will make a little more sense.0650

Let's say we have our Person object, and we have our hello function.0659

Now, we have created a new method; you can have as many methods for a class as you want.0662

We are calling it getFirstName, and we just want it to return the first name of this Person object.0668

Now, we could access that using our typical accessor syntax; but for the sake of this lesson, and learning the material, we are going to do it through the method.0675

And what you do is: in order to access the firstName property of this particular Person object,0685

you use the this variable, and then you use the accessor syntax, followed by the property name that you want to get.0695

For example, if in our test script this.php, we define two Person objects.0703

We set both the firstName and lastName of each of the different objects.0715

And then, we are going to call this getFirstName method that we have defined on each person.0720

For example, person1 is defined...we set the firstName equal to Joe, and lastName equal to Smith.0726

When we call this getFirstName function, what it is going to do is invoke this getFirstName function.0733

And it is going to return the value of the property firstName of the this variable.0740

Now, the this variable changes for each particular object instance; so each object instance...0749

every time you call this getFirstName function, this is reassigned a new value.0755

So, when we call this function on our person1 object, this is going to refer to person1.0760

In a way, it is as if we replace it with person1/Person.0768

But the way you do that within a class definition is: you just use $this.0775

Now, the reason for doing that is that now we can reuse that.0779

If I go back and type it again...if we had person1 here, now this function would only work with a variable named person1.0783

By using this special variable $this that can change its value, depending on which instance of an object was being used,0789

we can reuse this method, and so it is going to work for every object we create of this type.0797

For example, person1--we had set their firstName equal to Joe, so when we call getFirstName on person1,0807

this in that method is going to refer to person1, and it is going to get its firstName property, which is Joe.0814

Now, when we go down here on person2, and we call getFirstName, that this variable, when that method gets run,0821

is going to refer to this person2 object, whose first name is Jane, and it is going to return that property.0827

And so, what that does is: it is the same method that gets run, and has the same code, but it returns different values,0836

because each time that method is called, that this variable gets reset to the variable of the object that you are working on.0842

For example, when we run this script, what it is going to output is the name of both of our different people.0855

So, if we run this.php, we can see that the Person firstNames are Joe and Jane.0865

If we look at our code, we can see that we just had two echo statements0869

that called the getFirstName method on person1 and getFirstName on person2.0876

So, you can see that each of those methods returned a different value.0880

It returned the firstName value for that particular instance of that object, because0885

when the method is called, that this variable changes to become the instance of the object you are operating on.0890

Now, one thing to notice is that, for example (and this is a required thing):0898

let's say, in this function, we wanted to access this firstName property.0902

Well, you might think that you could just access it using this $firstName, because you would say, "Hey, we have this class;0906

it has the firstName variable defined in it; let's just return firstName."0913

If we go ahead and try and do this, and we run our script, we are going to get an error saying that that variable, firstName, is undefined.0920

So, it is very important that when you want to access the properties of a class within a method, you use the $this keyword.0929

So now, when we set it back to what it was before, it is going to work.0943

So again, because this $this variable is kind of an abstract concept, I want to draw a little diagram to explain it a little bit better.0952

For example, let's say we have a class that we have defined called X.0960

It has one property in it, and we'll just call it var1.0966

And it has one method that is just going to return the value of the var1 class property.0976

And it is going to use the this special variable.0995

What this essentially does is: when we define a class, we are defining a template.1012

We are saying, "This is a template for any variables that we declare of this data type (of this class, or this user-defined data type)."1015

So, we are saying that any time we define a variable that is of the class X,1024

it is going to have a property var1, and it is going to be able to execute this var1 function.1029

And so, it is code that defines a data type and defines methods that can be run on that particular data type.1035

And then, it can be used in multiple spots.1043

So, just as in our other application, we are going to define two instances of this X class and set var1 equal to 'A'...and 'B'...1045

Now, again, what is going to happen is: as this class is a template, when you create this new instance of the class X1080

and assign it to variable 1, this is saying that variable x1 is going to have this template assigned up here.1091

So, it is going to have a var1 variable; so when we assign var1 to it, it gets its own copy of that var1 variable.1098

So, each instance of a class (and this is an important concept, which is why I'm harping on it a little bit--it's that)...1104

every time you create a new instance of a class, you are creating a whole copy of this class here, in a new way, for that particular variable.1113

So, we are creating a whole copy of the var1 property, and we can set it to value A.1122

Now, when we create this x2 variable that is another instance of that class X,1128

we are creating another whole copy of that class, which includes its own version of var1.1134

Oops, I made a mistake down here.1139

When we set x2 var1 equal to B, x2's var1 property is completely separate from x1's var1 property; it is its own instance.1142

In the same way, for example, if we were to call this getVar1 method on our x1 object, it is calling its own version of that particular method.1153

And the way it does that is using this $this keyword.1172

As mentioned before, what happens when you call this getVar1 method is: this function up here gets replaced with...1175

I'll just use a shortened syntax without the public keyword...1185

So again, it is as if, when x1 calls this object method, it calls that method on itself--on its own instance.1210

So, what it does is: when this method gets run, this gets replaced, essentially, with x1.1218

It is just as if we were accessing that var1 property of x1 directly.1224

Now, when x2 calls this getVar function, this is going to get replaced with x2.1229

What this is reinforcing is this concept that the class provides a template of the properties and methods that a particular class can have.1238

And when you instantiate, or create a new instance of, each class,1249

you get copies of each of the different properties, and a new var1 property for each instance.1254

And you also get your own version of the getVar function.1261

And the way that you do that is using this $this method, which PHP is able to dynamically interpret when you call that function.1264

So now, we can define this getVar1 function one time, and it is going to work on any time we have an object of the class X.1272

One common operation performed on objects, a common method that is performed, is to get and set their property values.1284

We just saw an example, in our Person object, of something that is known as a getter.1289

It returned the value of our firstName property; it was called getFirstName.1294

So, getter methods retrieve the value of an object's property, and its setter methods set the value of an object property.1298

Now, we have been able to do this: let's say we had our person1 from before--we can set its firstName using this syntax here.1307

So, we actually don't need these getter and setter methods.1317

But it is a common thing that is done, and it is a special topic within object-oriented programming,1324

and within PHP, that we are going to get to in another lesson.1330

So, I just wanted to at least mention it--at least introduce the concept of it.1333

Now, we can set the firstName property of this person1 using this syntax that we use, the object accessor syntax.1339

But instead, we could create a setter method and say person1 (let's say we set firstName equal to Joe)...1350

we could define a method called setFirstName that passes it the value Joe.1365

And what that is going to do is: if this is a setter, it is going to take this value Joe,1374

and then, within the class definition, it is going to set the value of the firstName property by using the this variable.1379

We are going to see that in a second.1387

And one of the main reasons I am demonstrating this is that it shows how you can use the this variable1389

to not only return values from an object, and to access property values of an object, but also to be able to set them.1394

For example, if we look at a new version of our Person class, we have the same two properties: a firstName and lastName.1402

And then, we have defined what are known as getter functions for each of the properties.1411

We defined getFirstName and getLastName, and what they do is use the this special variable1415

to return the first name and last name, respectively, of this particular instance of a Person object.1420

And we also have setter functions, which take a parameter.1429

Again, object methods can take parameters, just as any other function does.1431

And then, here, we are using the this variable, and we are using the accessor syntax, to set the value of a particular property.1435

In this case, when this function is called on an object, we are saying, "On that instance of that object,1444

we are going to have the firstName property, and we are going to1451

set it equal to this firstName variable, which is passed in here."1453

This is how you can use the this function in order to also set object properties.1456

If we go and look at a test script, gettersSetters, we can see that we create a new Person object.1467

And now, instead of using the method that we had done before to set the first name and last name of the Person1473

(which uses this accessor syntax), we are going to use our setter methods.1481

We are saying person1: set firstName Joe; set lastName Smith; the same thing down here...1485

And it is going to perform the exact same function as when we had set them manually.1491

And when we go to output the person's first and last name, we are going to use the getters getFirstName and getLastName on each object.1494

What they are going to do is show that those values we set with the setter methods actually set those methods for those particular objects.1502

When we load this script up, we can see that it says Person #1's first name is Joe;1509

Person #1's last name is Smith; and the same for Jane Jones, who is Person #2.1520

And so, what you can see is that we have been able to set object properties within an object method (and that uses the this function).1527

And then, we are also able to access those properties, using the this variable.1536

And we do that using our getter methods, down here.1540

One thing I want to talk about now is: object methods are functions, and they return values.1548

And before, we have had strings where we have the value of a function that we want to output.1554

And you can't include that within double-quoted strings--you can't call a function within double-quoted strings.1562

However, you can include the output of object methods within double-quoted strings.1567

And the way you do that is using the curly brace syntax.1573

So, let's say we define a variable person1; we are going to define it as a new instance of the Person class.1576

We are going to use that setter we defined--we are going to set its firstName to Joe.1583

And then, we want to output the first name.1586

Well, what we can do is: if we want to output the value that getFirstName method on person1 object returns,1589

we simply call the method, using our normal method-calling syntax, and just enclose it within curly braces within our double quotes.1596

And this is opposed to having to use the concatenation operator (and I'll abbreviate).1604

It is a nice little trick that makes our code easier to work with: now, we can use methods within our string.1626

And that is something that...let's say this wasn't an object; normally, for a function, we couldn't do that anyway.1638

So, if we had a function called getFirstName (not an object method, just a function),1644

we wouldn't be able to include that, normally, within double-quoted strings anyway.1655

Let's take a look at this in action: we have our same Person object that we had in our last example.1662

It has the getters and setters for the first and last name.1668

We go ahead and instantiate two Person objects and set them with the same names we did before, Joe Smith and Jane Jones.1672

And now, what we are going to do is: whereas before we output their names using this concatenation operator1680

(we said Person, First Name, the dot operator, and then we called the method getFirstName),1685

now we know we can do it all within one string.1692

And so, instead of (actually, let's move it back again) having three separate strings here that we are concatenating together,1695

we can include it all in one; and you do that using this curly brace syntax.1701

This does the same thing as the last script does, except we are making use of including the outputs of an object method within a double-quoted string.1707

If we look at what it looks like when we browse to it, it is going to look exactly the same; and you can see that here.1717

One final topic that I want to talk about is using the $this variable...you can also use that to call other methods from within object methods.1730

For example, when we were in our (let's take a look at our Person class) getLastName,1742

we could use $this to refer to the particular instance of the object...1751

Well, when this method runs, this gets set to whatever instance of that object you are running the method on.1756

And we use this to access that lastName property of that Person object.1763

We can also use $this to call object methods from within itself.1770

For example, we have our function hello down here; before, if we look at the very first version of it, it just returned a string.1776

Well, now what we are doing is calling our function hello; and within our function hello, we are going to say, "Joe says hi" or "Jane says hi."1791

And the way we are going to do that is by making use of this getter function we defined for this Person class.1800

And we are going to call it within this object method.1806

And the way we do that is using this variable again.1808

When we create a variable, a Person variable, and we set its first and last name, and then we call the hello method (for example)1812

on person1, what is going to happen is: hello is going to get called; $this is going to get replaced with the person1 object.1823

And then, it is going to say, "Call the getFirstName method on it."1831

Well, getFirstName is defined within the class; it will go up here, and it will say, "return $this-->firstName."1835

So then, $this will also know that it refers to Person object 1, because PHP does that for you automatically.1840

It is going to get the firstName property of it and then return it.1848

So then, it is going to put that name here, and then concatenate it to this "so-and-so says hello."1851

This is an example of how you can use the this variable to also call object methods from within other object methods.1857

You can access object properties using the this variable, and you can also call other object methods within them, as well.1866

If we run this script, we can see that now we get an output message that says "Joe says hello" or "Jane says hello."1873

When this ran, and this function is called, we are calling on this object the getFirstName method.1886

So, on person1, we are calling getFirstName; when we go to getFirstName,1894

we are returning the firstName property of the person1 object that is replaced by this.1899

And then, that is how that gets output.1905

Now, this isn't a particularly real-world example; something that is a little more real-world is maybe...1907

because we are typically not going to be using getter and setter functions, as you will see in a future lesson...1914

something that we might do is: maybe we will define a function called getFullName.1919

Because fullName is not a property of a Person object, you can't access it using the accessor syntax.1923

For example, let's just say this was in a random script: you couldn't say fullName, because fullName is not a property of person1.1932

What we can do, though, is create a useful method that is going to return the full name.1946

And what that is going to do is concatenate the first name and the last name, which are properties of that object, and return that.1949

And that might be a useful function: we are going to say, "Give me the full name of this person."1955

If we define an object method in our Person class called getFullName, it will return a string.1960

And this is using our curly brace syntax, again, to output...well, in this case, it is outputting object properties within a string.1966

We are saying, "When you call getFullName, you are going to return a string that is going to contain1973

the value of the firstName property of this particular object, followed by a space, followed by the lastName property of this object."1981

And so now, in our function hello, we can make use of this function.1990

You can make use of it externally, but we are going to make use of it internally here,1996

to again reinforce the idea that you can call object methods within other object methods.1999

We are going to update our hello function to now call $this-->getFullName.2004

So, when hello gets called, it is going to say, "On this object, call the getFullName method."2009

And then, it is going to return a string that has the first name and last name (concatenated together) of this particular object.2016

And then, we are going to concatenate that 'says hello,' and so hello is going to2025

return a string that is going to say 'Joe Smith says hello' or 'Jane Jones says hello.'2031

If we look at the output of this script, we can see 'Joe Smith says hello'; 'Jane Jones says hello.'2036

That is just a combination of all the different things that we have learned,2045

and something that is maybe a more practical use of how you would see these object methods used.2048

For today's homework challenge, it is kind of an advanced homework challenge.2060

I'm going to have you create a class called HttpRequest that is going to represent an HTTP GET request message.2063

And that is something we learned about in an earlier lesson in the course.2071

I want this HttpRequest class to have two properties: one called uri, which is a string2076

(and that is going to represent the particular URI that this particular requested object would be requesting),2082

and then a headers property, which is an associative array that is going to contain2088

name/value pairs of any HTTP headers for any particular HTTP request this HttpRequest object would be representing.2094

And then, now that we have our properties, I want you to add a method to the class called addHeader.2102

It takes two parameters (in this case, they are called name and value),2108

where the name represents the name of a particular HTTP header, and the value represents the value of the header.2113

So, for example, the name might be host (which is an HTTP header), and the value might be educator.com.2119

What this addHeader method is going to need to do is: it is going to need2130

to be able to access this headers property of the HttpRequest object in order to add this name/value pair to it.2134

And so, in order to do that--to access, from within an object method, the properties of that object--we need to use the this variable.2142

So, you are going to have to use the this variable within this method here.2149

Then, I want you to add a method called getHeaders; and what it is going to do is return a string in proper HTTP request format2153

that represents all of the headers that are in this headers array up here.2163

So again, you are going to have to use the this variable to access this headers array.2166

And what you are going to do is loop over the array, and then output each header on its own line, with the name and value separated by a semicolon.2170

And so, that is going to be a method that is going to be used internally within the function.2178

Finally, I want you to define a method called getRequest.2182

And this is the method that is going to be used by people that are using this class.2186

They are going to instantiate an HttpRequest object; they are going to set its URI; they are going to maybe add some headers using the addHeaders function.2190

And then, they are going to call the getRequest method, which is going to say,2198

"Give me the formatted HTTP request that is represented by the URI and headers of this particular object."2201

What getRequest is going to do is create the required first line of an HTTP request,2210

which has the method name (in this case, it is going to be get) of the URI and the HTTP version.2219

And then, it is going to call the getHeaders method to append all of the HTTP headers to this first line that it creates.2224

This is an example of an object method calling another object method.2233

In this case, getRequest is going to be calling the object method getHeaders.2237

Then, just in a separate script, I want you to instantiate an HttpRequest object.2242

Set the uri property to index.html (and you can just use the object accessor syntax).2248

And then, add (using the addHeader method) two HTTP headers: one called Host, and one called User-Agent (which are both real HTTP headers).2255

You can set them to whatever value you want--it is not as important for the example,2264

but just the fact that you are going to be practicing using object methods.2269

And then, call the getRequest method on that object and output what it returns between pre tags,2274

which is going to show the output formatted in this form down here, which is like a computer text format.2281

And then, verify that your output has the form shown here.2290

For example, this first line of yours should be exactly the same as this: it should say GET /index.html HTTP/1.1.2293

And then, it should have two header lines, one for Host and one for User-Agent.2302

And their values should be set to whatever values you supply to that addHeaders method.2305

Notice that they are formatted properly with a colon and a space in between here.2310

And you are going to be doing that in your getHeaders method.2315

And so, for this, you need to make sure that you are appropriately using the this variable2321

to call, for example, the getHeaders method from within the getRequest method,2327

and also to be able to access that headers variable and the uri variable.2333

For example, when we are outputting this first line in our getRequest method, we are going to have to have access to the uri property of this class.2337

So, as a little hint, one of the things that you are going to need to do is use the $this-->uri in order to access that.2346

So, just make sure that you properly use the this variable, and that, when calling the methods,2355

you also practice using that same accessor syntax that we used to call methods.2361

For example, let's say we had an object of HttpRequest called http; to call the getRequest method on it,2366

as you will remember, we use this dash, greater than sign, the name of the method, followed by parentheses.2382

In this case, getRequest doesn't take any parameters, so it will just be empty parentheses.2387

That ends today's lesson; thank you for watching Educator.com, and I look forward to seeing you next time.2393

Educator®

Please sign in for full access to this lesson.

Sign-InORCreate Account

Enter your Sign-on user name and password.

Forgot password?

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.