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!
Loading video...
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

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!

Web Application Development

  • The property values of an instantiated object can be output within double-quoted strings using curly brace syntax:
    • echo "Item Name: {$item1->name}";
  • Object properties can be of any data type, including both arrays and other objects themselves.
  • The values of object properties that are arrays can be accessed using the following syntax:
    • Indexed Array Property: $dept->items[0]
    • Associative Array Property: $cart->items['1001']
  • The values of object properties that are arrays can be output within double-quoted strings using curly brace syntax:
    • Indexed Array Property: echo "Department Item ID: {$dept->items[0]}";
    • Associative Array Property: echo "Cart Item Quantity: {$cart->items['1001']}";

Web Application Development

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:11
    • Lesson Overview
  • Version 5.0 Overview 0:50
    • Version 5.0 Overview and Examples
  • Outputting Object Properties 12:38
    • Outputting Object Properties
  • Array Object Properties 14:18
    • Access and Set Array Value

Transcription: Web Application Development

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 development of our web application, incorporating what we have learned about objects so far.0005

Today's lesson is going to cover version 5.0 of the web application.0013

And as said, it is going to cover inserting objects into our web application, changing it0017

from an application that modeled items and departments, for example,0022

as associative arrays into one that models them as actual PHP objects.0026

We are going to have a quick lesson on how to output object properties within double-quoted strings.0032

And then, we are also going to talk about accessing object properties that are arrays,0039

and how to use the square bracket syntax to access values in those arrays.0043

So again, what we are doing is taking these items and departments objects that we have in our application;0052

before, they weren't official PHP objects, but we modeled them as an associative array.0058

For example, we would pass around to different functions an item associative array0064

that contained a number of different keys (for example, name that would represent the name of a particular item).0070

What that allowed us to do was to pass around all of the information about a particular item in one variable--in this case, item.0077

Well, now what we are going to do is officially create that Item data type, that Department data type, by creating Item and Department objects.0084

So now, first of all, instead of passing associative arrays between functions to access item information,0095

we are going to be passing Item and Department objects in between functions,0100

and we are going to be accessing the Item and Department objects, rather than arrays.0105

What that is going to do is change how we access our item properties0108

from the square bracket syntax (like is used here) to the object property accessor syntax,0112

which is the dash followed by the greater than sign, which we learned about in the last lesson.0119

For example, if we have an Item object that had been instantiated using the object accessor syntax,0123

now this is how we are going to access the property name, or how we are going to refer to it to set its value.0133

Let's go ahead and take a look at what our Item and Department classes look like.0142

Here you can see, in our documentation that is generated by PHP Documentor, that there is a new section on the left-hand side here called Classes.0149

And what it does is provides documentation about the different classes in our web application.0158

You can see the Department and Item classes; you also see the Customer class,0161

which we are going to talk about in a second, which is another class we have defined for this web application.0165

If we look at the Item class, and we look at the source code for it, we can see that we defined this class called Item with a capital I; that is the convention.0172

And if you look at the structure of our web application, the name of the file that this class definition is defined in is called Item.php, with a capital I.0181

It is in our Classes directory, which is in our Includes directory.0190

And again, this follows the one-class-definition-per-file convention, where we only have one class definition in each file.0193

And we can see that we have defined five different properties for an Item class.0201

It has a string property that is the itemID; it has string properties for the name, the description, and the image file extension.0208

And then, it has a float property for the price.0215

And you can see here, we have defined it using the public keyword, as we had talked about in our last lesson.0219

And so, that represents an Item object now.0224

So now, instead of using the associative array to access all these properties, we are going to access them as an object.0228

If we look at our Department class, we can see that it has three properties: deptID and name are two of them (both of which are string properties).0236

We can also see that it has the items property, which is actually an array.0246

It is an indexed array, if you will remember, of all of the item ID's that represent all of the items in a particular department.0250

What this shows is that the properties of a class can be of any PHP data type.0258

Here, we have strings, an array, an Item class; we have strings and floats.0263

And you can even have properties that are objects themselves, which we will see in some later lessons,0268

where properties defined in a particular class will be data types that are other objects, or other class types.0274

And so, what we have done is: now that we have created all of these different classes, or this Item and Department class:0284

we have to go through our web application and update all of the functions that used the associative array syntax0290

for accessing our Item and Department information, as well as any of the pages that accessed that information using associative arrays.0298

And it has been done in a number of different spots; there are a lot of spots on our application that have done that and been updated.0305

And we are not going to be able to go through all of them in the lesson.0311

But all of the changes are noted in the change log for version 5.0.0314

But I am just going to go through and talk about a couple of different changes that were made.0318

For example, if we look at our file library, we can see that now we have a new function called buildDeptObject.0324

Whereas before, when we read a line from departments.txt, and we used the explode function on it0333

to extract all the information about a particular department, we used a function0341

(if we look at our last version's documentation) called createDeptDataArray, and what it would do0345

is create this associative array that represents the department;0351

well, now instead of creating a departments array, we are going to create a Department object.0356

So, we have gotten rid of that createDeptDataArray function.0360

We also got rid of the createItemDataArray function, and we replaced them with buildDeptObject and buildItemObject, respectively.0365

And, as you can see--for example, in the buildDeptObject, we still take in an array that contains information in a line in departments.txt.0375

And now, instead of creating an empty array for our department variable, we defined a new Department object using the new keyword.0383

And then, we used the accessor syntax to go ahead and set its department and name values.0393

And then also, down here, we use it to set its items property to the array of all the different items' ID's.0399

And so, this is what we are going to use now to build our Department objects from our departments.txt file.0407

Likewise, the buildItemObject works in the same way; it is just that it defines a new instance of an item object.0415

And then, what it does is sets all the different properties; it just has more properties that it sets.0424

For example, now our createDeptDataString function has been updated.0432

Whereas before we passed in an associative array of department information, now we are passing it...0437

If we look at the documentation for it, we can see that the parameter that it takes0442

is still called department, but it is actually a Department object--0447

whereas in our previous version, we could see that department was an array0450

(and as it says here, it is an associative array defining information on all of the information in our department).0459

One thing to note is that, for example, in our createItemDataString, this is our new version--version 5.0.0471

And we can see that what it does is: this one takes an object now.0485

So now, we are moving on to the string; this takes an Item object and creates an item string that we are going to store in our items.txt file.0490

Similarly, createDeptDataString now takes a department object.0497

And what it will do is create a string to store in our department data file.0502

As you can see, it uses this syntax to access different properties of the department, as well as the different properties of a particular item.0506

We have just gone through and updated all of the different methods.0517

For example, getDepartment now returns a Department object, as opposed to an associative array.0520

You can see, in the documentation for this, that it returns a Department object.0525

getDepartments returns an indexed array containing a Department object for each department in our store.0531

For example, if you look at the documentation, it says that it returns an array.0542

And the array is of Department objects, whereas before it was an array of associative arrays describing those different departments.0547

That is another thing to note: that arrays can contain any data type, including...0555

we know that they can contain other arrays, integers, booleans, all the scalar data types...0560

But they actually can also hold objects, as well.0566

So, in this case, we are returning an array of Department objects.0568

This is the pattern for the rest of these functions, as they have all been updated to use the Department and Item objects.0574

Additionally, if we look at (for example) Item.php, we can see that this is one of our pages.0586

Now, when we call getItem at the beginning of Item.php, it is going to return an Item object.0595

And so, you can see that now, when (for example) we set the title of our Item.php page,0602

we are using an object accessor syntax, whereas if we look (for example) at an older version of our web application,0609

and look at Item.php, you can see that we use the associative array syntax.0616

We have updated all of the functions to use objects and their objects' accessor syntax, as well as all of our different pages that previously accessed associative arrays.0622

One other thing to note is that we also created a Customer class, which is used with the checkout.php form.0638

If we look at our old version of checkout.php, we can see that we had this custDataArray that we used0645

to represent all of the information submitted on the checkout form, when the user submits their shipping address, and so forth.0658

For example, it has the street address, their first name, their last name, and so forth.0667

And it was an associative array, and we used it in a couple of different functions.0671

For example, in our emailOrder function, we would pass it this custData associative array,0675

and emailOrder would be able to extract information about that.0682

Well, now we have updated our emailOrder function: instead of using this custData array, what it is going to do:0685

if we look at version 5.0, we can see that what we have done is:0695

now, when we call the emailOrder function, we are passing it a Customer object.0708

And you can see up here that we create the Customer object within this data processing section.0712

So, we declare a new instance of the Customer class, and then we set its different values.0717

And if we go and look at the code for our Customer class, we can see that it just0724

has all the same properties that our custData associative array used to have.0734

It has (they are all strings) properties for first name, last name, street, apartment, city, state, and ZIP code.0737

So now, we have encompassed all of the information about a customer's shipping information into a Customer object that we can pass around.0744

Any functions that used that custData associative array before now use the Customer function.0752

One topic that I wanted to talk about was how to output object properties within double-quoted strings.0759

We know that you can output arrays within double-quoted strings (for example, an associative array).0766

This is supposed to be a curly brace.0773

We can output using this curly brace syntax.0779

Well, we can also do the same thing for object properties.0784

For example, let's say we have an echo statement, and we want to output, as a string, the name of this Item #1 object.0787

Well, the way you do that is: you use the accessor syntax on the item1 to get the property you want,0795

and you just simply enclose it in curly braces.0801

For example, if we go look at item.php, and we look down at...there is a call down in the output section0805

that outputs the item's image, and one of the things that you have to pass to the outputImg function0814

is the name of the image file that you want to output.0819

Well, the way we build that up is from the item ID, followed by a period, followed by the image file extension of that particular item.0824

Well, here, you can see: we have created a string, and using the curly brace syntax,0830

we were able to include the item ID and the image file extension of this particular item that this page represents.0837

So, currItem is an instance of the Item object.0843

And by enclosing them in curly braces, we can output that within double-quoted strings.0847

That is a nice feature of being able to output object properties within strings.0853

Another thing that we had mentioned earlier is that object properties can be arrays themselves.0860

For example, in our Department object (let's look at the source code for it), we have this items property, which is an indexed array.0865

What we can do is: if we want to access values in that particular array that is a property of that object, there is a particular syntax that we use.0881

For example, what you do is: you use the accessor syntax to access that particular array property.0892

For example, if we had dept1 (which is a Department object), the way we would get access to that items array0904

or the items property would be to use this accessor syntax.0910

And then, what you can do is directly append to the end of it square bracket syntax, just as we did with any other array to access different values of it.0913

For example, to access the first item in the items property of this dept1 object,0921

you just append the square bracket syntax right after you access the particular property.0929

Essentially, it is the same as if we had gone and created a temporary variable, and said $a=$dept1-->items, and then accessed it as $a[0].0941

We can do that directly in one statement, by just directly appending the square bracket syntax.0959

It also works for associative arrays, too, so this could be a string key.0964

And you can do this to set values, as well.0969

In this case, we were getting the first element of this items array of the dept1 object.0971

In this case, we are setting the first element of the items array of the dept1 object, just using an assignment operation.0977

And if we look at our addItemToDept function that has been updated to use objects,0986

we can see that addItemToDept is what we have been using to add an item ID to the items array of a particular department.0995

So here, the way that we do that is: we access that items property, which is an array.1006

We use the accessor syntax; and then, we just append our square bracket syntax to it.1012

And square brackets, as we know, with arrays--if you don't put anything in between them,1017

and you use them in an assignment statement, it just says to add another element to that array.1022

So, this is saying, "Get this items array; append another element to it; and have it have the value itemID."1026

Now, this is opposed to if we look at addItemToDept in our last version.1034

We can see that we used a multidimensional array syntax, because our department object was represented as an associative array.1038

That is an example of how you can directly (in one statement, as opposed to two up here) access values in an array in an object.1050

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