Matthew M.

Matthew M.

Classes vs. Objects

Slide Duration:

Table of Contents

Section 1: 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
Bookmark & Share Embed

Share this knowledge with your friends!

Copy & Paste this embed code into your website’s HTML

Please ensure that your website editor is in text mode when you paste the code.
(In Wordpress, the mode button is on the top right corner.)
  ×
  • - Allow users to view the embedded video in full-size.
Since this lesson is not free, only the preview will appear on your website.
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

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.
  • Learn at your own pace... anytime, anywhere!

Classes vs. Objects

  • A class provides a definition for a user-defined data type. An object is an instance of a class providing values to a class’s properties.
  • Each instance of a class has their own ‘version’ of a class’s properties, and these are referred to as instance properties.
  • Static properties are class properties whose values are shared between all of the instances of a particular class.
  • A property is declared static in a class definition by using the static keyword:
    public static $numPeople = 0;
  • An object of a class does NOT need to be instantiated to access a class's static properties.
  • The scope resolution operator (::) is used to access static properties.
  • A public static property is accessed from outside of a class definition using the syntax:
    Person::$numPeople; // uses the class name
  • A static property, whether public or private, can be accessed inside a class definition with an alternative syntax that uses the self keyword:
    self::$numPeople;
  • A static property CANNOT be accessed via an object instance variable using the -> operator.
  • Static methods are methods defined in a class that are at the class-level and are NOT associated with particular instances of a class. They are declared with the static keyword:
    public static function add($op1, $op2) {…
  • Static methods CANNOT use the $this variable within their definitions.
  • Non-static methods are referred to as instance methods.
  • Static methods are called using the scope resolution operator (externally using the class name, and internally using the self keyword):
    • Externally: Person::getClassName()
    • Internally: self::getClassName()
  • A class constant is a constant defined within a class definition that is specific to that class. They are defined using the const keyword:
    const PI = 3.14;
  • They are accessed using the scope resolution operator (with no $ prepending their name):
    • Externally: Math::PI
    • Internally: self::PI
  • Additional Resources:

Classes vs. Objects

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:09
    • Lesson Overview
  • Class vs. Object Review 0:58
    • Class vs. Object Review
  • Static Properties 2:42
    • Static Properties
  • Scope Resolution Operator (::) 5:26
    • Scope Resolution Operator (::) & Static Property
    • Coding Example: Accessing a Static Property Using the Scope Resolution Operator
    • Coding Example: Accessing a Static Property in a Class Definition
    • Coding Example: Using a Static Property as an Instance Counter
  • Static Methods 18:51
    • Static Methods Overview
    • Coding Example: Static Methods
  • Classes vs. Objects 26:28
    • Classes vs. Objects: Diagrams
  • Class Constants 30:24
    • Class Constants Definition
    • Coding Example: Using Class Constants
  • Homework Challenge 34:01
    • Homework Challenge: 1 - 5
  • Homework Challenge (cont.) 35:25
    • Homework Challenge: 6 - 10

Transcription: Classes vs. Objects

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

In today's lesson, we are going to be continuing discussing the topic of classes versus objects.0004

We are going to have a review of what classes and objects are, and the differences between the two,0011

because that is going to become important when we learn about what are known as static properties and static methods.0016

So far, what we have been working with (as you will find out) have been known as instance properties and instance methods.0023

They are properties of a class and methods of a class that relate to a specific instance of that class.0029

We are going to learn about something a little different; it is on more of a class level.0035

We are going to talk about an operator called the scope resolution operator,0040

which is going to allow us to access these static properties and methods that we are going to learn about.0043

And then, we are also going to learn about constants in classes.0047

We know about being able to define constants in our regular PHP files; we are going to learn about how to define them at the class level.0050

A class, as we know, is essentially a user-defined data type.0061

And when you create an object, what you are doing is creating an instance of that class.0065

You are instantiating a copy of that class, and you are filling in the values for the properties of it.0069

Essentially, what you have between a class and objects is a one-to-many relationship.0077

You have one class definition, and you can have as many different object instances of that class as you need.0081

The class provides the template, and the objects are the realization of that class.0088

But in the case of each instance--each instance is essentially (as we had mentioned) its own version of that class.0094

So, they have their own version of all of the properties.0112

For example, we have learned about object properties.0115

Object properties defined in a class definition get initialized and set when you create or instantiate an object.0116

Those properties are specific to that particular instance, so each instance of an object has its own copy.0119

For example, if a Person object had the firstName property, each Person object you create0123

would have its own copy of the firstName property, and it would be specific to that instance.0130

And so, for that reason (we haven't specifically called them that, but the distinction is going to become clear today), those are known as instance properties.0134

So, all the properties that we have been dealing with so far with our classes are known as instance properties.0141

They are properties that are specific to each instance of a class.0146

Each time you instantiate a class as an object, it gets its own version of those properties; and they will be referred to as instance properties.0150

Static properties are what we are going to talk about today.0164

Static properties are, in a way, like class properties; they are properties whose values are shared between all of the instances of a particular class.0167

So, whereas in your class definition--let's say, in our Person class--we have a firstName property;0181

well, that is an instance variable, if we define it as we have been...0188

And that means that it is an instance property, meaning that each instance of that class has its own copy of that property.0192

However, a static property is per class, meaning that there is one property defined in that class definition0202

that all of the different instances have access to, but it is not like they get their own individual copy.0209

So, if one instance of a class updates this static property of the class, it affects all of the other instances, in that they all read that same value.0215

The way you declare a static property in a class definition is using the static keyword.0227

Whereas before, we had just been saying public numPeople, and that would be our property definition;0233

now, we are added the static keyword: we are saying public static numPeople, and we are giving it an additional value of 0.0239

What this statement says is that we are declaring a public property that is a static property.0245

It is going to be called numPeople, and its initial value is 0.0250

And this would be contained within a class definition, just with all your other properties.0255

One other thing to note about static properties is: because they are not specific to an instance of a class--0260

they relate to the entire class, or all of the different instances of that class--0267

you don't have to have an instantiated object in order to access them.0273

Normally, for example, let's say we have a Person class, and it had a property firstName.0277

Well, to access that firstName property, we would write something like this.0290

And what this is: it is a reference to this instance of the Person object that was created.0298

Well, that is why it is called an instance variable, because this copy of firstName is specific to this a variable right here.0304

Well, because static properties aren't specific to a particular instance, you don't reference them using an object variable.0315

And we are going to see about that in the next slide--about how you actually access them.0323

What PHP provides is the scope resolution operator, which is two colons, one right after the other.0328

And that is how you access static properties.0334

For example, if we had a Person class where we defined a static property called numPeople0337

(just as in the last slide, public static numPeople equals 0), well, in order to access the value of that static variable outside of the class,0342

the way you access it is: you write the name of the class, followed by the scope resolution operator,0353

followed by the name of the variable--including the dollar sign.0359

And so, outside of a class, this would be how you reference numPeople.0364

And the reason you can access it: it is essentially the same thing as a class property,0369

because numPeople is a property of this Person class, because there is only one version of it.0375

There is not a version for each instance of the Person class.0378

And you can do that for public static properties.0382

In addition, if you are trying to access a static property from within a class method0385

(for example, if you have a class method defined within the Person class,0390

and you want to access the static property), you do so using this keyword self.0394

If this was, for example, a function or a method within a class definition, in order to access the numPeople static property of this class,0400

of this object, you would say self, and then you would have two colons, followed by the variable name.0411

And again, you include the dollar sign, which is a little different, because when we access instance variables,0416

and we use this arrow operator, and if we had a property called numPeople, we access it like this, without the dollar sign.0425

So, that is one definite distinction between accessing static properties versus instance properties.0436

And then, another thing to note is: outside of a class, you can't access a static property using the arrow operator.0445

So, for example, if we had this numPeople static property defined in a Person class,0452

we would not be able to access it using this arrow syntax.0459

Assuming a was an instance of a Person object, we couldn't use this arrow operator in order to be able to access this numPeople static property.0462

The way you do it is: you have to have the name of the class with the scope resolution operator.0474

Let's take a look at a basic example of static properties in action.0477

We have this Person class that we have defined.0484

It has two private instance properties, and these are firstName and lastName.0487

And they are particular to every instance of the Person class that gets instantiated.0490

So, every Person object that is created gets their own copy of these.0495

We have a constructor that takes two parameters: one is the firstName and the lastName that you are going to set for the Person.0498

Here, we set these instance properties.0504

And then, we are just also outputting a message and a constructor, just to say we are creating a Person (firstName and lastName).0510

Now, up here, what we have is a static variable.0515

You can see here, with the keyword static, we say public static; we are calling the static property for this Person class description.0518

And it is a description of the class; it says, "This class represents a person."0526

It kind of makes sense that, for any Person class that you have out there, if you want to get a description of it,0530

they all might have the same description: "This represents a person."0535

So, this may not be the most practical example, but it is used to demonstrate the use of a static property.0539

I'll show a little more practical example in a couple of examples.0546

If we look at a script called staticProperties.php, we have an include statement that includes that Person class that we just looked at.0553

And then, what we have (let me bring this down to another line) is just a message that says,0562

"I am outputting this static description property of the Person class."0569

And the way we access that is by the name of the class, the scope resolution operator (which is two colons),0578

followed by the name of the static property, including the dollar sign.0585

When we echo here, we are saying, "The value of Person::description equals" and then we are outputting it using this concatenation operation.0591

If we load this script up, and we take a look at staticProperties.php, we can see (let me blow it up a little bit):0601

Person::description--the value of it equals, "This class represents a person."0610

And if we go back and look at our Person class, we can see that that is the value that we had assigned to this static property.0616

That is how, outside of a class, you access a static property.0625

You use the class name, the resolution operator, and then the name of the property.0629

We had also talked about being able to access it from within a class.0635

So now, we have the same class here, except we have added what we are going to call (and we are going to learn about it in a minute) an instance method.0639

Up to this point, we have just been referring to it as an object method called getDescription.0651

And all it does is: it just returns the value of this class's static property description.0655

And as we mentioned in the slide, when we are trying to access a static property from within an object method, you do it using the self keyword.0662

When we are outside, we use the Person keyword--the name of the class.0674

You actually can do that within the class, but typically, what you do is use the self keyword,0677

because self means, "The class I am referring to is the same class of this object instance," which would be a Person class.0682

"And I want to get the description property."0690

So, what we have here is: we go ahead and include this new Person class definition and create two new people, Joe Smith and Jane Jones.0693

And then, what we do is: we are calling the getDescription instance method.0704

It is an object method; we are going to learn about static methods in a little bit, but for now, it's just a regular method like we have been using.0710

And what that is going to do is: as we saw, it is going to return the value of the static variable description.0716

So here, we are outputting the description static property for person1 and person2.0722

What we will see is that they are the same property.0730

And then, what we are going to do is change the property, using the syntax for accessing a static variable outside of a class,0732

where you use the class name and the scope resolution operator.0740

We are going to set the static variable description for Person equal to this new description: "No description available."0743

Then, what we are going to do is call this getDescription method on each of these Person objects.0749

And what we are going to see is that it is updated for both of them.0754

And so, in this example, we learned about how to access a static property from within a class method, or an object method.0757

And then, it also demonstrates how it is that one static property is for all of the different instances of a class.0765

When we call getDescription on this person1 and person2 object, and it returns that static variable,0772

they are both going to have that same value that they returned it to.0779

You can't have a separate value for the static property description for each instance of a class.0781

It is the same for all the classes of that type.0791

So, if we go and look at the script in our browser, insideClass.php, we can see "Create two Person instances," and there is some output from our constructor.0794

We created a Person Joe Smith, and Jane Jones.0808

And then, we can see that, when we first try to output the static property, which is before we changed it0811

(it was immediately after we created these Person objects, so for example, we instantiate the Person objects,0817

and then we immediately output the static variable description and the static default value for that variable,0823

"This class represents a person), it outputs "This class represents a person."0830

Now, when we go back and look at insideClass after we have changed it to "No description available,"0836

when we go back and run that same getDescription method that is going to return the value of the static variable description,0840

both objects are going to return that same string, "No description available."0847

And we can see, the second time we call it: they both output "No description available."0853

Somewhat of a more useful example of using an instance property is an instance counter, sometimes called a reference counter.0860

It is keeping track of the number of instances that you have of a particular class.0868

For example, each time you create a Person object, if you have a Person constructor, that constructor is going to get called.0873

What you can do is increment this; for example, here we have a static variable for the Person class called numPeople.0880

And this variable is the same for all instances of the Person class.0887

So, every time in our constructor, what we do is increment it by 1.0892

Every time we create a new Person object, we are incrementing this static variable numPeople by 1.0897

So, any time outside the class that we reference this static variable Person::numPeople, and we see what this value is,0901

it is going to equal the number of times we have run this constructor.0909

And it is going to be the same across all different classes, so when we increment numPeople here,0912

it doesn't just do it for any particular instance of this class; it does it for all of the Person classes.0916

We just changed our constructor a little bit to increment this counter variable called numPeople.0924

And then, the other thing that we can do is that we can create a destructor that, when the destructor is run--0931

when the object is destroyed--it decrements the numPeople static variable.0937

So, every time we create a Person object, the counter is going to go up by 1.0941

Every time we get rid of it, the counter is going to go down by 1.0944

We have a script here where we defined three different instances of a Person object that all have their own firstName and lastName instance properties.0948

But they all share the same numPeople static property.0961

And then, we have a statement here that just outputs, after we have created all these, a statement that says,0966

"The current number of Person instances is" and then we access that static variable Person, numPeople.0971

In this case, it is going to say 3, because we have created 3 Person objects.0978

The constructor is called 3 times, and for that case, it has been incremented 3 times.0982

Then, what we are going to do is get rid of one of the Person objects; we are going to set it equal to null.0988

That is going to implicitly call the destructor, which is going to decrement it by 1.0992

So now, it is going to say "The current number of Person instances" equals 2, when we again try to reference Person, numPeople.0996

If we go and look at this script in the browser, we can see an outline of what happens.1003

Before we even created any of the objects, I had an output statement that output that static variable for Person, numPeople.1014

Even for a class, even if you haven't instantiated an object of the class Person, you can still access its static properties,1023

because they are specific to the class, not to a particular instance.1030

If we go back and look at the code, I missed mentioning this line here, where before we have even created any Person objects,1035

we are still able to access this numPeople static property of the Person class,1043

because it is on a class level, rather than an instance level.1050

And if we can see, we had initialized it to 0; so when we first run this output, it is going to say "The current number of Person instances is 0."1054

Then, we say we are creating three Person instances, and it shows us the three people we created.1064

When we output, now, the value of that static property numPeople, it is going to say 3.1069

We destroy a Person instance--we get rid of the first one, Joe Smith.1075

And then, when we output the static value again, numPeople, we can say the current number of instances is 2.1079

And so, it shows that it was able to decrement.1085

And down here, you can see: these destroying messages come from our destructor.1087

And the reason they are down here is because we know, when a script ends, PHP destroys all of the objects,1092

or calls a destructor on any objects that were still being used.1098

And so, since these objects were in use, their destructors get called automatically.1102

So, even though we didn't specifically call them, that is why they show up here at the bottom.1106

And so, that is somewhat of a more practical example of how you might use a static variable1110

to keep track of, for example, the number of instances of a particular class.1116

And the reason you can do that is because it is the same for all of the different instances,1121

and when each one updates that static variable, all of the other ones know about it.1125

Now, we are going to talk about something called static methods, which I hinted at a little bit.1132

And it is essentially the analog of...where static properties are to instance properties, static methods are to instance methods.1136

And instance methods we haven't talked about, but what we have been declaring so far and defining in our class definitions have been instance methods.1145

They are methods that you call using an instance of an object.1153

For example, if we have an object a, and we wanted to say get name, this is an example of an instance method.1157

It works on an instance of a particular object.1171

It is going to have access to the instance properties of this particular instance, whatever class it may be.1174

And you use the arrow operator to get access to it, because it is specific to an instance of a class.1181

A static method, on the other hand, is not specific to an instance of a class.1188

And we are going to see that you access it using the scope resolution operator, as well.1193

The way you declare a static method is just like you do any other method in a class, except you include the static keyword.1199

And because static methods are not associated with a particular instance of a class, they can't access the $this variable within their definitions.1207

Now, this should make sense, because if you remember, the $this variable, when we use it within an object's...1218

what I want to refer to as instance methods, $this refers to this particular instance of an object.1226

Because a static method doesn't relate to an instance, it doesn't know about this.1233

It doesn't know about any particular object; it just knows about the class definition as a whole.1239

So, you can't access this within static methods, or you will get an error.1244

And as mentioned, the non-static methods, which is what we have been using up until now, are called instance methods.1249

And static methods are called using the scope resolution operator, just as we did with the static properties; we are going to see that in a second.1255

Outside of the class, you preface it with the class name.1264

For example, if getName was a static public method, we would call it like this from outside the class.1266

Within a class method, we would call it using the self keyword.1280

And again, note that we are using this scope resolution operator, as opposed to the arrow operator,1292

because we are not accessing a particular instance of a class.1297

Let's take a look at...I created this class called Integer that is sort of an object that wraps an integer value.1301

It has one private instance property that is called intVal, and it just represents an integer.1310

So, when we call our constructor, we pass it an integer (1, 2, 3, whatever), and then our constructor sets intVal equal to that integer.1319

We have an object method, or an instance method, called add, in which we can pass it an integer as a parameter.1327

And what it is going to do is access whatever particular instance we are calling it on (this instance)--1337

it is going to access its current int value, add it to whatever you pass in, and then return it.1344

If we look at our code, here we are creating an Integer instance, just as we do normally, using the new operator.1351

We are passing it that we want to set that class equal to (in this case, 1).1365

And then, we are adding 1 to it; and we are saying, "The sum of 1 and an integer with the value 1 equals..."1370

and then we are calling the addInstance method and passing it 1.1376

So, we are saying, "Add 1 to our current integer," which is already 1, so it is going to output 2.1380

Here, you can see: because this is an instance method, it uses this arrow (or dash, greater than) operator to access that instance method.1385

And it does so on a variable that has been set to a new instance, or to an instance of the Integer class.1395

Now, on the other hand, we could define a static method in this class--a static keyword called integers.1403

We simply pass it two integers, and it just returns the addition of them.1410

So, it does the function for us; but we don't actually need to access any object properties.1414

We don't need to access any instance properties, whereas up here, because this was an object method1420

and we wanted to add something, we accessed what the current integer value1425

of this Integer object was, and then added to the value that was passed in.1433

Now, this is like a stand-alone function, but within the context of a class.1437

So now, if you want to add 1+1, instead we could access that static method addIntegers, and pass it the parameters 1 and 1.1442

And the way we call it is by prefacing it with the name of the class and the scope resolution operator.1451

This is saying, "Call the addIntegers static method defined in the Integer class, and pass it the parameters 1 and 1."1459

And it is going to return the addition of those.1467

So, when we view our script, we are going to see the sum of 1 and our Integer object that has int value equals 2.1468

The Integer value equal to 1 is going to equal 2.1483

And then, when we call the static method, the sum of 1 and 1 equals 2.1485

So, in this case, we haven't acted on an instance.1489

Now, one thing to note is that I mentioned that, just like with static properties, you can access static methods within an object, as well.1492

And so, in this case, we actually could do this.1504

If we wanted to, we could call this addIntegers method from within our add function, and have that do all the work.1508

It doesn't really make sense, but this is just an example to show how you would access a static method within another class method.1517

Use the self keyword, followed by the scope resolution operator, then the name of the method.1525

And then, in this case, we would pass it 2 values, because it is a function that takes 2 integers.1533

In this case, we are going to pass it the integer value of this particular instance of the object.1538

And then, we are going to pass it (to add to it) the integer that we pass in to this add method.1544

Now, when we go ahead and run our script, we are going to see that the same thing is output,1552

because if we look back here, when we call int1-->add(1), what we are doing is saying, "Call the add instance method on this instance of the integer."1555

It is going to go to our Integer class; it is going to call this add method, which then calls this static method addIntegers.1564

And it gives it the value and says, "Add the current value of this Integer object, plus the value that we passed in."1571

And when we refresh our script, it is going to show the same thing.1577

That is a way to also access a static method within another object method.1581

Just to try and sum things up in maybe a more visual fashion, we have this class/object diagram that we looked at a little bit when we first introduced objects.1591

Let's say we have this Person; this is a class that we defined in a class definition file somewhere.1604

And this is shorthand; this isn't correct syntax; but let's say we have defined three properties for this Person class.1608

firstName and lastName are both instance properties, so they are particular to each instance of a class.1615

And then, we are defining a static property, called desc, which is short for description.1622

And so, that is going to be a property that is the same for all of the different instances of a Person class.1627

Then, we are going to define two methods: we are going to define an instance method called getFullName,1633

which is going to operate on a particular instance of a class.1638

And it is going to have access to the instance properties of a particular class.1641

We are also going to define a static method called getDesc, which is just going to return the value of this static variable description.1645

And so, this is a static method.1655

When we create...for example, if we use the new keyword, Person, every time we did that, we would be creating an instance of the Person class.1657

This is an instance of the Person class; in this case, maybe we would assign it to the variable called person1.1669

So now, we have an object, and an object is an instance of this class.1674

And an object gets its own copies of instance properties.1678

We are going to assign Joe, for example, to the firstName instance property, Smith to the lastName instance property.1682

In addition, it gets its own copy, in a way, of getFullName.1689

So, when you call getFullName on this instance (maybe you would call it like this...and maybe it would return the concatenation1693

of firstName and lastName), this method is an instance method that has access to these instance variables.1702

Essentially, what I have shown is that here, we have just created two objects, person1 and person2.1709

You can see that the properties that are instance properties and the instance methods1714

get their own copy made in each instance of an object that is created.1720

However, this static method stays up here; and so, when you access a static method or a static property,1725

it is almost like you are calling them on the class itself.1734

Whereas down here you would access the instance properties or instance methods using the arrow operator;1737

now we have this new scope resolution operator to access static properties--in this case, description.1743

And that is how we would access that.1753

It is kind of like all of the things that are static stay in the class.1756

It is almost like a class, in a way, is an object of itself; it has its own properties, and it has its own methods.1760

In this case, it has its own description method that is specific just to that Person class.1766

It has its own getDescription method, which is specific to that Person class that anybody can call (assuming they are public methods).1771

But it doesn't have access to these instance properties of these instance methods.1778

Those are particular to each new object that is created.1783

Again, this is kind of a tricky concept, which is why I am focusing on it a lot.1788

It is just that when you create an instance of a class, any non-static properties or non-static methods1795

(which are instance properties and instance methods) get their own copy created for that particular instance.1803

The static properties and the static methods don't get copied; they have one copy1809

that resides in this global class definition that all of the objects have access to.1814

And it is shared between all of the instances of the objects.1820

One other related topic is class constants.1826

We know how to define constants within our regular scripts.1829

Well, you can define a constant within a class that is specific to that class.1832

You do it with a const keyword, and then you define the constant as you normally would.1835

Here, it is just alphanumeric characters--the same rules for naming a variable, except there is no dollar sign.1842

And the convention is still to capitalize all of the letters of a constant's name.1850

And then, you provide it a value.1855

So here, we are declaring a Math class, and we are declaring a constant in this Math class called PI,1857

and setting it equal to the approximate value of pi, 3.14.1863

The way you access a class constant: because it is a constant, it can't change, so it is going to be the same in all the different classes.1867

So, it makes sense that you would access it kind of like a static property.1874

And you do that outside the class using the scope resolution operator again.1877

So, you have the class name (in this case, Math), and the scope resolution operator, followed by the name of the constant.1883

That is how you would access that from outside the class.1890

Inside the class, you would use the self keyword.1892

And then again, notice: the one thing here is that there is no dollar sign.1895

If PI were a static property, you would access it maybe like that after the scope resolution operator.1899

That is one key thing to note between static class properties and static class constants: the absence of the dollar sign.1907

Let's take a look at an example of that.1916

I created a class called Circle; we defined a constant in it, PI.1919

PI equals 3.14; and then, we defined a static function, which is a function that is just particular to the Circle class.1924

Anybody that knows about the Circle class can access this public static function.1932

And what it does is calculates the area of a circle.1939

You pass it the radius, and the formula for the area of a circle is pi, r squared (pi times the radius times the radius).1941

What this function does is accesses this class constant PI using the self keyword in the scope resolution operator.1949

And then, it says to multiply it times the radius twice.1957

You can call this function in a stand-alone way, and you call it the same way that we learned about accessing static methods.1961

And you would call it by using the name of the class, Circle, the scope resolution operator,1969

the name of the static method that we want to call, and then also the parameter that we are passing.1973

We are saying we want the area of a circle with radius 2.1980

This is an example of calling this area static method on a Circle class that makes use of this constant PI.1983

And then, up here, we just have an output statement that shows how we access the class constant PI outside of the class.1991

And we use Circle (the class name), the scope resolution operator, and then the name of the constant.1998

And that, again, is different from when we access it within a class method, because here we use the self keyword.2005

So, if we were to go ahead and load this, we can see that it outputs the value of PI as 3.14.2013

So, we were able to access that class constant, using the scope resolution operator in the class name Circle.2022

And then, we were also to calculate the area of a circle with radius 2 by accessing that static method called area,2028

using the Circle class name, the scope resolution operator, and area.2034

For the homework challenge, what I would like you to do is build upon the Circle class that we just talked about from lecture example version 5.2043

And what I would like you to do is add an instance property to this class, called radius, that is going to represent the radius of a circle.2050

And add a public static property called numCircles, and we are going to use that as an instance counter.2057

What we are going to have you do is create a constructor that takes a radius as its parameter.2065

And what it is going to do: it is going to set that radius instance property that you are going to be declaring equal to whatever value you pass in.2069

And then, also, in the constructor, it is going to increment the static property numCircles by 1.2079

So, that is going to keep that reference count, because that numCircles is going to be for all of the Circle classes.2085

I also want you to create a destructor that, when it is called, decrements numCircles by 1.2091

And so, every time a object is created, that static variable numCircles is going to be increased by 1.2096

Every time an object is destructed, it is going to be decreased by 1.2101

Then, just output the value of the constant PI, which is already defined in the class that we saw.2105

And that will just give you practice accessing a class constant using the scope resolution operator.2110

Use the static area method, just like we had in our example, to output a circle with the radius of 2.2117

And then, I want you to create an instance of the Circle class.2127

Pass the constructor a value 2; so what it is going to do--the Circle constructor should take the value 2 and set its instance property radius equal to 2.2131

And then, create a calcArea instance method, which is going to calculate the area of a particular instance of a Circle object.2144

This is going to be an instance method, not a static method.2157

It is going to have access to the $this variable, so you will be able to access the radius using this, followed by the arrow, followed by 'radius.'2162

Use that, and within your calcArea method, you will be able to grab the value of the radius property2173

of that particular instance, calculate the area, and return it.2182

And when you call it--let's say you defined a circle variable and you call it circle1--you would be calling this method using the arrow operator.2186

And note that we don't have to pass it the value of 2, because the circle object already has a radius property defined2199

that already knows about it; and that is how you access it with this.2206

Repeat steps 6 and 7 for another Circle; so basically create two Circle objects.2211

Then, output the value of the static property numCircles after you have created these two, and you should verify that the value is 2.2216

That shows that you are properly incrementing that static variable that is keeping track of our instances.2222

Then, destroy one of the Circle objects by just setting it equal to null.2228

And then, re-output the numCircles property and verify that it has been decremented to the value of 1.2231

And so, that will give you practice with accessing the static variable outside of a class, so you would be accessing numCircles.2237

This homework example will give you practice accessing class constants, class static variables or properties, and class static methods.2254

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

Educator®

Please sign in to participate in this lecture discussion.

Resetting Your Password?
OR

Start Learning Now

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

Membership Overview

  • Available 24/7. Unlimited Access to Our Entire Library.
  • 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 lecture slides for taking notes.
  • Track your course viewing progress.
  • Accessible anytime, anywhere with our Android and iOS apps.