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

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

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

Sign up for

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

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 7.0 Overview 0:39
    • Version 7.0 Overview
  • Item Constructor 2:19
    • Item Constructor
  • Department Constructor 7:47
    • Department Constructor
  • Customer Constructor 12:23
    • Customer Constructor

Transcription: Web Application Development

Hello again, and welcome back to'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 learned in our last lesson on constructors.0005

In today's lesson, we are going to be providing an overview of the new version of our web application, which uses constructors, which is version 7.0.0013

And then, we are going to go over all of the different constructors that we have created.0020

We have three classes that we use within our web application: the Item class, the Department class, and the Customer class.0024

And we are going to discuss how those constructors work for each of those classes, and how they are used within the web application.0033

As mentioned, for this version, 7.0, constructors have been created for all of our objects that we have (Item, Department, and Customer).0042

And everywhere where we have created instances of the objects, before we used the new statement.0053

Now, we are using the constructors that we have created.0063

And all of our constructors, as you will see in a minute, take arguments.0065

Whereas before, for example, we were just creating a new Item with a statement like this, with something over here or a variable,0069

now, because we have created constructors that take arguments, we will be creating instances of objects using this syntax over here,0078

where we will actually be passing argument values to our constructors.0092

And that is something we learned about in the last lesson.0095

Additionally, for all of the constructors we have, we are providing the ability to set all of the different property values for that object.0099

For some of our constructors, values are not known.0108

For example, when we create a new Item object from our addItem.php page, we don't know what the itemID 0111

and the image file extension are yet, because those get extracted and created in the insertItem function.0120

When we initially create these Item objects, we are going to be passing null for some of the parameters, because we don't have values for them yet.0129

The Item class that we have--we have created a constructor for it, and it takes arguments for 0141

each of the different properties that the Item class has, like itemID, name, price, description, and so forth.0147

And the way we work inside the constructor is: if any properties are not supplied to the constructor, or they are empty,0155

then we just set those properties to a default value of an empty string.0163

And if you remember from our lesson on constructors, that is part of what constructors are used for: to set up an initial state of a particular object.0166

And in this case, we are making sure that all of our values for an Item class have at least a basic value set.0175

Let's take a look at the Item class in a new version, 7.0.0186

And we can see that we have created a constructor (let me blow this up a little bit), which, as we know,0195

is a magic method, which is named __construct.0201

And as we can see, the signature, or the prototype, for this method is to take 0207

a string ID, a string name, a float price, a string for the description, and a string for the image file extension.0214

And if we go down and look at our constructor here, this is the body of the constructor.0222

All that it does is goes ahead and, using the $this keyword (which we know allows us 0227

to get access to a particular object's instances' properties), we are going to set (for example) the item ID.0234

And if an item ID is passed in (so it is not empty), we are going to set it equal to the item ID passed in.0243

Otherwise, we are going to set it equal to the empty string.0248

We do this for all of our different variables.0250

And what that does is at least gives them a baseline value; for example, they will all at least have an empty string value, so that they are at least initialized.0254

The different places that this new Item constructor is using: the first place, for example, is in addItem.php.0268

So, if we take a look at addItem.php, and we look at the old version (which would be version 6.0),0274

when we create an Item object in the data processing section, we previously had just used the new statement that said new Item().0283

And because we hadn't declared a constructor before, no constructor gets called.0293

So then, what we would do afterwards is manually go ahead and set each of the different properties of this new item--0298

for example, the name, the price, and the description.0303

Well, now these statements can all be incorporated into one constructor method call.0306

So here, we say new Item, and we have our parentheses as we normally do when we are declaring a new instance of an object.0314

But now, we can provide argument values for all of the parameters of the constructor.0321

If you look back at our documentation for the constructor, we can see that it takes five different parameters for each of the different properties.0327

Because of that, we have to pass it five different parameters.0336

In this case, right now, because when we use our addItem form, we haven't generated an item ID yet, it has to be gotten from the lastAdded file.0339

And we haven't gotten the item's image file extension, because we extract that from the image file that gets uploaded.0348

We do that in our insertItem function.0355

We only have the name, price, and description for which we can specify the constructor.0359

But because it takes five arguments, we need to provide values for the other ones.0364

And in this case, one of the standard ways of doing that is just providing the value null.0367

So, this creates the Item object and sets these three properties.0372

And it does the same thing as this here, because of the way our constructor works.0377

The other spot that we use our new Item() constructor is in buildItemObject, which is a function in our file library.0384

If we go look at buildItemObject, we can see that what we do is: again, here is an example of our new constructor.0392

It has the opening and closing parentheses, and then, in between it, it has five different arguments.0402

In this case, when we use this buildItemObject class, we are building an item up from data fields that were extracted from a line in our items data file.0407

So here, you can see: for example, for the item ID, we are pulling the first data field from a line in our items file, and we are running the trim operation on it.0417

And this allows us to go ahead and set the ID, the name, the price, the description, and the image file extension for this particular item.0428

Now, because we are pulling this buildItemObject function to build an item object up from a file in our items text file in our database,0435

that items file already has all of the information about a particular item.0449

So, you don't have to use null here for either the item ID or the image file extension,0454

which is why all 5 of the parameters to this Item constructor are passed values.0459

We have basically done the same thing for our Department class: we have created a constructor for it.0469

And it takes arguments for all of the different properties that a department has: its name, its department ID, and the items,0474

which is an indexed array of all of the item ID's of the items in the department.0482

What we do in the constructor is: for the deptID and name property, if those are not passed a value when the constructor is called,0488

they get set to the empty string, which is similar to what we did in the Item class for any properties that were not passed values.0497

For example, if we pass in the value of null, that is considered empty; we are going to set a value for the properties0503

equal to the empty string, just so that they have at least an initialized value.0509

Additionally, if the items property is not passed in (if we don't pass in an indexed array of item ID's that represent items in the department),0514

then we are going to be initializing items to an empty array.0524

And that is because it is an array property, and so, when we initialize it, we want to initialize it to a data type that it is going to be used as.0528

In this case, it is an array, so we use the empty array.0538

If we quickly take a look at the constructor for our Department class, we can see that we have construct.0542

It takes a deptID, name, and items parameter.0551

And what it does is: as said, it sets the department ID and name equal to whatever values were passed in, if they were passed in.0557

Otherwise, it sets them equal to the empty string.0564

And then, it also tests if items was passed in; and it also performs an additional test.0566

If items is an array, then it goes ahead and sets items to the array that was passed in.0572

Otherwise, it just sets the items property equal to the empty array.0576

This is another example of one of the things that the constructor allows you to do.0581

When you create an object, it allows you to perform some data checking on any information that code provides to your particular constructor.0585

What that allows you to do is make sure that your object stays in a state that you understand, and that you know about.0596

For example, if we hadn't checked this is_array, we could have passed in a string here.0602

And then, if we just ran the test to see if it was empty, then we would be setting our items property--0608

we would be initializing it to a string value, when, in fact, it should be an array.0614

This is an example of how we could use a constructor to make sure that our object always starts off in an appropriate state.0618

And like with the Item constructor that we created, we use our new Department constructor in the buildDeptObject function.0626

If we take a look at that, what we can see is (I'm trying to make it a little bigger) that, when we build our department object,0637

we call the constructor, and we provide it a department ID and a name.0650

And in this particular case, we use buildDeptObject again to build a department object from a line in the departments.txt file.0656

And the way we do that is: we initially pull the department ID and the name for the department and set it in the department object.0663

Then, what we do is loop over that last data field in departments.txt, which is a comma-delimited list of all of the item ID's in the department.0669

So then, what we do is use the addItem function (if you remember) 0678

to add each of those item ID's to the items array of the particular department we are building.0682

Because we essentially do that after the department is already built, for the items parameter of the departments array,0690

we initially pass it the value of null; and that is kind of like what we did for the items array.0696

Then, what we do is loop over all of the item ID's that are part of that third data field in each line of departments.txt.0702

And then, we use our department addItem function to add each item ID to the items array.0712

And if we go and look at that again, we can see that all the addItem does is checks to see if an itemID is already in the department;0718

and if it is not, it adds that to the items array.0733

So, between the combination of using the constructor and then this addItem function, we are able to build up our Department object.0735

The last class that we currently have in our application is the Customer class.0745

And we have created a constructor that takes arguments for all of the properties of that class:0749

first name, last name, street, city, apartment, and so forth.0754

And for any properties that aren't supplied when we call a constructor, we set them to the empty string.0757

And again, that is just part of using the constructor as a way to initialize all of the properties of our new object,0762

to make sure that it is in a well-known state.0769

And if we take a look at our Customer class, we can see that we have the constructor method that takes 0773

7 different parameters for each of the properties (as you can see here)--the string properties that are part of a Customer class.0783

And if we look down at our construct method, it looks very similar to our item class, in that it tests to see if any particular parameter passed in was set.0792

If it is, it sets the object's property value equal to the value passed in; otherwise, it sets it equal to the empty string.0802

And the place that we use this new constructor is in checkout.php, which is where we 0810

build up this Customer object from the data a user submits when they are checking out on our web application.0815

If we go and look at checkout.php...first, let's look at the old version, version 6.0.0822

We can see, in the data processing section, where we are completing the order0828

(which is after the user has entered all of their shipping information), that what we did in the last version was:0831

we created a new Customer object, a new instance, and we used this default syntax.0837

Because we hadn't defined a constructor, no constructor, in this case, is able to get called.0844

So, what we have to do then is manually go ahead and set each of the different properties of this new Customer object.0850

And in this case, we are doing it from data that was passed in by the POST method.0859

So, we are accessing this custData associative array, which contains all of the information about our new customer.0864

Now, in our new version, we have done the same thing, except we have incorporated this all into one statement.0871

It is a call to the Customer constructor.0877

So, instead of just calling Customer with two parentheses, as we did before, we call it with parameters.0880

And what it is doing is passing the firstName, lastName, street, apartment, city, state, and zipCode, all to that new Customer constructor that we created.0886

And then, that constructor is going to be able to go ahead and set all of the properties of that particular Customer object.0896

And so, now we have turned it into this one statement that created a new instance.0903

And then, all of these other setter statements for setting each of the properties are now combined into one constructor method call.0910

And because it does that, that would also allow us to, within the constructor, make sure that valid values were passed in.0921

For example, if we wanted to make the application a little more robust, we could do things0928

like to check to make sure that the zipCode is a valid ZIP code.0933

We could check to make sure that the state is only two characters, and it is a valid state abbreviation, and so forth.0936

And so, again, to reinforce: that is what the purpose of the constructor is--to get your new object that you are creating initialized,0945

and get it in an initial state in which it can be used, where all of the data contained in it is valid data.0952

Or if no proper data was provided, it at least has default values that you can work with in other functions of that class.0960

That is how we used all of these different constructors within our web application.0970

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