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 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

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 6.0 Overview 0:40
    • Version 6.0 Changes & Examples
  • Item Methods 0:53
    • Item Class Definition: getImageFilename()
    • Coding Example: getImageFilename()
  • Department Methods 7:33
    • addItem(), removeItem(), and isItemInDept() Method
    • addItemtToDepartment() & buildDeptObject() Functions
    • A Default Value Needed to Be Set for $items

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 learned in our last lesson about object methods.0005

We are going to provide an overview of the new version of the web application, which is going to be version 6.0.0013

We are going to provide an overview of the changes.0018

And the changes for this version of the application are that we have added some methods 0020

to our Item and Department classes that improve the functionality of our code.0026

And then, we are going to specifically talk about the methods we have added to the Item class, and then the methods we have added to the Department class.0032

As mentioned, we have just added methods to both our Item and Department0042

that are going to allow us to learn what we used in the last lesson, and also to improve the functionality of the application.0047

For our Item methods, we have added one method to the Item class called getImageFilename.0056

And what it is: it is like a helper function...because we know that an Item's image's file name is the ID of the Item,0063

followed by a period, followed by the property image file extension, every time we have been, for example,0074

trying to get the name of an Item's image, we have had to concatenate those two together: 0082

the ID and the image file extension, with a period in between.0085

Well, now we can create this helper function that is going to put that functionality in one function.0089

So, any time we want the file name of an image, we just call that particular function.0095

It is kind of like in our last lesson, when we talked about the getFullName method that was able to get the full name of a Person object0099

by combining the firstName and the lastName and putting it into one method, so that every time you want to get0107

the full name of a Person, you don't have to access two separate properties; you can just call one method.0112

So, if we take a look at getImageFilename, if we look at our Item (oops, that was our last version) class,0121

we can see that here is a definition of our Item class.0135

But now, we have an object method defined down here.0137

In this case, it is called getImageFilename, and what you can see that it does is: it simply returns a string0142

that is the concatenation of the item ID of this particular instance of the object, concatenated with the value of imageFileExt.0147

For example, it might be 1001, followed by a period, followed by jpg.0156

Here, you can see, as we learned in our last lesson, that we use the special variable $this 0162

to access the properties of a particular instance of the Item class from within the object's methods.0168

So now, any time we want the image file name, we can just call this method; and we can see that this is going to be useful in two separate places.0179

For example, in our Item.php page, when we output the image of our Item, or we call the outputImg function,0187

and we manually extracted the itemID property of this current Item that this Item.php page is showing the information for,0198

it added the dot and then went ahead and added the image file extension.0211

So, we are manually doing this operation.0214

Now that we have a function that does it for us, that simply gets replaced with a function call.0217

Now, we have currItem, and we call getImageFilename.0223

And here, you notice that this is the syntax we use for calling object methods; it is the dash followed by a greater than sign,0227

and then the method name followed by parentheses, with nothing in between them if there are no parameters that need to be supplied to it.0233

And so, one thing you can see already is that it cleans up our code a little bit.0241

So, instead of having this mess everywhere within our code, now we have it in one file name.0244

And all of that ugly code is consolidated to this one spot here, within our Item class.0250

Additionally, we can see that, because we have included this in the one spot, we can reuse this, as we always do when we create functions.0258

One of the goals of creating functions and methods is to reuse them.0266

So, if we look at our insertItem function, one of the things that the insertItem function does 0270

is moves an uploaded image file from the temporary directory to the images directory.0280

Part of what it needs to do that is the file name of an Item's image.0285

As you can see down here (here it is), we have the call to move_uploaded_image.0292

And then, one of the parameters that move_uploaded_image takes is the image name of the Item.0306

And so, here we can see: we use this getImageFilename object method on this particular Item that you are inserting into the database.0313

So, as before (if we looked at the old version of this, insertItem in our last version), you can see down here that0323

here we have that same concatenation operation done here in hand.0337

We have this ugly code, and now we have replaced it with a simple function call.0342

We have been able to reuse the functionality; we have been able to clean up our code a little bit.0348

Another thing that having object methods does is: it provides you the ability to change things.0353

So now, let's say we had an architectural decision, and decided, "We don't want to call the name of an Item's image its ID plus the file extension."0358

Maybe we want to call it the Item's name plus the file extension.0373

Well, then, in every spot in our code, we would have to go in...0376

For example, here in insertItem, we would have to update this so that here, it would access the Item's name.0379

And then, we would have to go to Item.php, and we would have to update this here, so that it would say current item name.0389

We have to replace that in every spot that it is used.0398

Well, now that we have incorporated this into a function, we don't have to change that function call at all.0401

All we can do is just go change that function's definition.0406

We could go to (let me pull up the code for it) our Item.php class.0409

And right now, it returns the itemID; all we would have to do would be to change one line in here to name.0424

And now, every time a function needs the file name of an Item's image, it is going to get the correct version.0429

That is another advantage of using object methods.0437

And as with any functions, we can consolidate the functionality to one spot.0440

And then, if we need to update it, we only need to update it in one spot; so, that is one of the advantages of that.0447

For our Department object, we have added three different methods.0454

The first two are addItem and removeItem; and what they do is: as we know with our Department class,0457

it has a property that is an array called items, and it is an indexed array 0464

that contains the item ID's of all of the items in that particular department.0469

Well, we are creating these helper methods that are object methods that you can call on a Department.0473

For example, you can call addItem, and you can pass it a value of, say, 1001; that is saying, "Add item 1001 to the Department."0479

This method will go and know how to update the items property of that particular object,0487

so that item 1001 would now be added to it.0496

removeItem does the same thing, except that it removes a particular Item that you pass to it from an Item Department.0499

We don't make use of that right now, particularly in our script, but because we have added the addItem functionality,0506

we are going to add the removeItem functionality, too, because it may be something that we are going to be using in the future.0511

And it also shows another example of looking at object methods.0516

If we go and look at our addItem method in our Department object (oops, go to the right version),0522

we can see that what it does is takes an item ID, and then (I'll get to this in a second) it tests to see if the Item is in the Department.0534

And then, if it is not, it goes and adds the item ID to the items property.0542

Here again, you can see the $this variable, that special variable, that is going to say,0548

"When this addItem method is called, on whatever Department object instance it is called on,0553

I want you to add the itemID to that object's Items array."0561

removeItem does the same thing, except it just unsets, using the unset construct in PHP to remove a particular Item from an array.0568

And it does that by searching through the Items array in that particular object.0580

Now, one thing to note is that the other method that we have added to this class is called isItemInDept.0587

And if we look down here, it is a simple function: you pass it an item ID, and it tests to see if that item ID is already in that items array for that particular Department.0595

So, essentially, it is just testing if that Item is already in that Department.0610

We are making use of this PHP in_array function, which tests for a value in an array.0614

And we are saying, "Does the value that we are passing to it (itemID) exist 0619

in the items property array of this particular object instance that we are working on?"0624

When we call addItem, say, with string 1001, the first thing we do is: we call this method.0630

And here is an example of calling an object method from within another object method.0638

We are saying, "On this object, call this isItemInDept method, and see if the itemID we passed to addItem is in the items array."0642

Now, the key thing here is that it uses this $this keyword, so that, whenever this method addItem is executed0652

on a particular object, it is only going to test for if an Item is in that particular instance of the Department.0663

Remember, with the $this variable, it refers to a particular instance of an object.0672

So, each time this method is run on a different object, the values in that items array could be different.0677

This isItemInDept function may return true or false differently, depending on the values of the items array in that particular instance of that particular object.0685

That is the addItem function and the use of the isItemInDept function.0699

One of the places that we have used these is in our addItem function, which adds an Item to our store database.0707

And then also, we have used them in addItemToDept and buildDeptObject.0715

Let's go take a look at the addItemToDept function.0726

And I misstated: the addItemToDept function uses this new function, addItem, we have created, as well as this isItemInDept function that we have created.0732

If we go look at our addItemToDept, we can see that...0741

Well, first of all, let's look at the old version of it.0747

What we used to do is: we would load a Department from the database, using this getDepartment function.0750

We would check to see if the particular item ID specified to the function was in the function, by looping over this items array.0758

And we did that using this object accessor syntax.0767

And then, if it wasn't in the Department, we went ahead and added the item ID to the items property of that particular Department.0773

Here is an example of the square bracket syntax for accessing or adding an element to an object property that is an array.0782

Well, now what we have done is: in our updated version, our test is simply:0790

we had to test to see if the Item is already in that particular Department.0796

After we load the Department up, we just call this function we have created, isItemInDept,0800

which is going to test to see if that item ID is in the Department.0805

If it already is, the function is going to return true, because addItemToDept (if you remember) 0809

returns true if the function is successfully added to the Department, or if it is already in there.0814

If this returns false, then we are going to go ahead and add the Item to the Department.0819

And before, we did that directly, using this array syntax.0823

Now, we do it using the addItem method, and we specify itemID.0829

Now, again, the one advantage we can take from using these object methods is...0836

Well, let's say, for example, we wanted to do some verification on an item ID, because in this case here, let's say0847

maybe somebody else writes some code that wants to add an item ID to our items property of a particular Department.0855

Well, they can add an item ID that is an invalid ID; let's say item ID's have to be strings comprised only of numbers--they can't have letters.0864

Well, with this, they could go ahead and directly add an item ID to this items array that contains letters in it.0871

Well, now, calling this addItem function--requiring that to be used to add an Item to a Department0883

(and we will see more about requiring that when we talk about getters and setters),0891

what we can do is provide some validation within addItem.0896

So, we could go and...let's look at our new version of Department.php...where we have our addItem array...0900

Well, first of all, we do a little bit of validation: we test to see if the Item is in the department, so that is one thing.0915

And if it is not, we add it to there.0921

But also, we could perform some other tests in here.0923

So, let's say, we could perform a test that would say, "If this contains any letters in the item ID, then don't add it to the items array."0925

So now, instead of having to perform that testing everywhere that we want to add an Item to a department,0940

we can include it in this function, and it will get run every time that this addItem object method is called.0948

That allows us to, any time an Item is (attempted to be) added to a department, perform some validation on it.0957

And we can include that all in that class definition.0965

We can include that validation, as opposed to maybe having it perform a check here that says, "Are there letters contained in here?"0967

and then, any time else we used the addItem...maybe we used it in another function 0974

where we would want to add an Item to a department--we would have to perform that same check--0978

we can include that all in the addItem function.0981

One thing that I wanted to note, also, is that a default value needs to be set for our items property of our Department object.0988

If you look at our Department class definition, you can see that we have set a default value here.0997

And we learned, in our first lesson on objects, that you can do that.1004

And we did that because, in order for this isItemInDept to work on newly-created objects, 1009

you have to have a default value set, or else you are going to get an error.1018

So, we set that up here; that is just one thing to note.1021

We are going to be addressing that in our next version of the application, when we are going to learn about what constructors are.1024

So, we are going to move the initialization into what is known as a constructor function.1030

But I just wanted to mention that to you--that we have added that initialization.1034

And without that, there are going to be errors that will show up when you try to call these functions.1037

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