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
Lecture Comments (1)

0 answers

Post by Richard Rupsis on February 23, 2012

Download Training Files: Starting with Web Application
version 9.0 all 'Output.php' files start with short
tag '<?' instead of '<?php' which will cause errors
if you don't have your ini file set for short tag.

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
  • Version 9.0 Overview 0:12
    • Version 9.0 Changes & Examples
  • Output Class 1:08
    • Output Class & Static Methods
    • outputHtmlSelector ( ) Method
    • outputItemSelector ( ) Method
    • outputDeptSelector ( ) Method
  • buildObject() Methods 11:54
    • buildObject() Methods & Examples

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

incorporating what we learned in our last lesson about static methods and class constants.0008

In version 9.0, which is the version of the web application that we are updating to,0015

we are going to be creating an entire new class called Output, which we are going to use0019

to contain some static functions that are helper methods that are used to perform some simple, useful HTML output functions.0023

We are also going to be changing: previously, we had a buildItem object and a buildDept object function in our file library0033

that are used to build up a Department object or an Item object from a string that is read from items.txt or departments.txt.0041

We are going to be moving that functionality into each respective class, and calling it a buildObject method.0049

For example, Item is going to have a buildObject method now; it is going to become a static method.0057

And it is going to perform the same functionality, but it is going to be a static method within the Item class; likewise for the Department class.0062

First, we have the Output class that we have created, and it contains a couple of static methods for performing some common HMTL output tasks.0070

For example, a dropdown menu is a common HTML output.0080

And what we are going to be doing in this version is: we want to select an item from the database, maybe on an admin page.0087

And actually, I will just go to the page that I am going to show you.0093

In our administrator website, for example, what we are doing is creating dropdown boxes 0098

that allow us to select the Item that want to add to the Department, and the Department we want to add it to.0106

Previously, we just had a text box where we were able to enter the item ID.0112

Well, now what we are doing is dynamically generating these select boxes by reading from our store database,0115

from those flat files, and then generating those; and we are using these static functions to do that.0122

We have a general static function that we are creating called outputHtmlSelector, which just will return a well-formed select tag0129

containing all of the options that you specify for it.0137

We are going to look at that more in a second.0139

And then, we have two specific static functions for outputting a Department selector, 0141

to select any of the different Departments in the store, and also an Item selector, to select any of the different Items in the store.0148

And both of these methods call this generic outputHtmlSelector method, in order to be able to do that.0156

If we go take a look at our new Output class, we can see (for example): we have an outputHtmlSelector class method.0164

It is a static method, and what it does is builds up a select tag.0177

You provide it two parameters; one is the name of the select tag (and the select tag is an input tag, so it has a name,0183

so that when you submit the form that it is on, it is going to associate whatever option is selected with this name).0189

So, it is going to associate the value of the option selected with this name.0195

You pass it the name; then, we also pass it an array of options, which is going to include 0198

the value that we want to include in the value attribute of each option, as well as the text that we would like to include.0203

For example, if we look at our administrator website, here what we have (let's look at the source code)--0211

we can see that the output of this function is: we create a select tag with name itemID.0224

And then, for each option value, we have read from the database (and we'll go over that in a second) the item ID.0231

And then, we have output the name of the item and its item number as the text of the option tag.0238

For example, here...the format that the options array needs to be in is an associative array 0252

where the keys are the values that you want to include as the value attribute of each option, 0260

and the values of the array are the text that you want in the body of each option.0266

You can pass it any name in any array of any value and option body text pairs.0271

It will go ahead and generate all of those options within a select tag.0282

It will close it out, and then it will return that whole string--the select tag with all the different options in it.0286

What we do is make use of this in two other static methods.0294

One is called outputItemSelector, and what we do for that one is load all the items from the database.0297

This getItems method, as we know, returns an indexed array of Item objects--of all of the Items in our database.0303

And then, what we are going to do is build up an options array to pass to this outputHtmlSelector method that we just looked at.0310

What we do is: the keys of this options array are what we want as the values for each option tag.0317

So, what we are doing is: for each item that we pull from the database, we are setting the key in this options array equal to its item ID.0324

And then, we are setting its value equal to a string that contains both the item's ID and the item's name.0333

When we look at it, you can see that we have the item ID (which is 1001), and then the name.0340

And so, we do that for each item in the store, and it is going to build up this options array.0350

And then, we call that static method; and here we are using the self keyword.0356

And as we learned in the last lesson, that is how you call a static method from within another method of the same class.0360

We have self, followed by the scope resolution operator, followed by the name of the static method we are calling (in this case, outputHtmlSelector).0373

We want that select tag's name to be itemID, so that when the form gets submitted, 0381

whatever value is chosen is going to be associated with the name itemID.0387

And then, we are going to pass it this array of options that we built up, which contains values for the option tags,0391

which are the items' ID's, as well as what text to output, which is a combination of the item ID and the item name.0396

Now, we have also done basically the same thing for departments.0403

We load all the departments from the database into an array, and then we build up an options array.0408

And what we do is set the key of each option array to be the department ID of each department.0414

And then, the text that we are going to have for each department is going to be the department's ID, followed by the name.0419

And then again, we call this static outputHtmlSelector that is in the same class as this method is defined in.0425

So, we use self and the scope resolution operator.0435

We give it the name deptID; that is the name we want the select tag to have.0438

It is the value we want for its name attribute.0441

And then, we pass it this options array we have generated that contains the department ID's as the values, 0443

and then a string of the department ID and the department name as the text.0450

So, when we go and look at this, we can see that our output is the department ID, followed by the department name.0456

And so, what we can do now is: instead of just knowing what an item ID is and having to type it,0464

we are able to (and this will change any time we add a new item to the database) is going to automatically update it to this select box,0469

because this select box calls getItems, which draws from the database.0477

For example, let's just show what this looks like first.0481

If we look at our store, in the Sporting Goods department we have two items: a baseball bat and a basketball.0486

Now, let's say, just for fun, we want to add the winter jacket to that Sporting Goods department.0491

Well, we simply select Winter Jacket and Sporting Goods, Add to Department.0496

It is going to say, "Winter Jacket was successfully added to Sporting Goods."0501

If we go back and refresh our Sporting Goods page, we can see that Winter Jacket is now in that department.0504

It is the same functionality we have had before, but we have made it easier to use.0509

Now, let's say we go ahead and add another item to the website.0513

Let's see, a soccer ball--$25...a description...pick a random image...I'll just give it the image of the basketball, since that is one we have on here.0518

Add the item to the store; the item was successfully added--it has been assigned a new number.0539

If we go back and we look at the store, we can view our cart; we can see that this soccer ball item has been added--it's item 1007.0544

However, you would think it would be in the Sporting Goods department; 0555

but when we go to Sporting Goods, it is not in there, because we have not added it.0557

With our new interface, now when we click Add Item to Department, it is going to automatically load that new item.0560

Refresh the page, and now we can see that Soccer Ball has been added to the list.0567

We dynamically generated this list, where we can select any item we want from the store.0571

It is quite a neat feature; and we can add it to our Sporting Goods department and click Add to Department.0577

Go back and refresh our Sporting Goods page; we can see that that new item has been added.0583

So, we have taken the same functionality, but we have made use of the static functions 0587

that we have included in this Output class to dynamically generate these select tags.0592

And so, what this Output class is, if we look at the source for it, is simply just a class of public static functions that you call using the...0601

Actually, I haven't shown you how to do that.0616

If we go look at Add Item to Department, previously we just had text fields for the item ID and the department ID.0618

Now, instead of outputting a text field, we output these select boxes.0628

So, if we go down here, we can see, in the same spot: we are calling these static methods defined in the Output class.0632

We are saying, "Call the static method outputItemSelector defined in the Output class, and output the result of that method."0639

In this case, it is going to be a select tag.0648

We do the same thing down here for the department: call the static method and outputDeptSelector in the Output class, and return an outputted value.0650

And here, again, the way we access static methods from outside the class definition is by typing0659

the name of the class, followed by the scope resolution operator, 0665

and then the name of the function that we want to call and its parentheses, and if optional, any parameters.0669

That is how we make use of this Output class.0677

And these are functions that we could have just added to our...for example, we already have an outputLIB file;0680

we could have added these functions to here, as well.0689

But sometimes you will see that classes are defined with just a bunch of static methods in them; it is a different way of doing things.0691

And so, we are showing you that you can have some output functions, just as regular functions defined in the file library;0700

or you can have them defined as static methods within, for example, an Output class.0707

The other main thing that we did in this version of the web application is: we didn't get rid of, but we moved,0715

the buildDepartmentObject and buildItemObject methods that were in our file library.0722

We made them static methods called buildObject within the Item and Department class.0731

For example (let's close this), in our previous version of the web application, in our file library,0738

we had buildDepartmentObject that would take an array of information extracted from a line in departments.txt.0745

It would create a new Department object, and by reading the information from the array, it would set the ID of the department.0755

It would set the name of the department; and then, it would loop over each of the different ID's and add those to the department, using the addItem method.0773

Similarly, for buildItemObject, we would pass it an array of information extracted from a line in items.txt.0782

And we would simply build up an Item object using the Item constructor, and then return it.0789

What we do in the new version is (you will see in our file library): those functions no longer exist.0795

And if we go, for example, and look at item.php in our Item class, we can see we have a new function called buildObject.0801

It is a static function that returns an Item object, or false if there is an error.0810

And it takes a string, which is an Item string.0814

So, what we have done is: we have added the part that...0816

Before, what we had been doing is reading from the items.txt file, using the explode function0822

to take all of the data fields into an array, and then passing it to our buildItemObject function.0828

Now, we are just making it so that we are passing a line directly to this buildObject method.0833

We pass it an Item string, which if you remember...these Item strings that we output in our file 0838

are generated by the __toString method that we had added in a previous version of the web application.0845

And that defines the way that an object will be represented as a string, and that is how we store them in our file.0851

Whereas __toString converts an Item object to a string, this buildObject is going to take that Item string and turn it into an Item object.0858

And so, we have this static method buildObject.0869

And you just simply pass it an Item string; it calls explode on the Item data.0872

And then, it goes ahead and builds the object and returns it.0878

Now, one thing that you may notice is that the other thing that we have done is: 0881

we have moved the data separator constants from config.php into the Item class.0884

For example, if we look at the Item source code, we can see that now there is a constant declaration here:0892

const keyword, followed by the constant name DATA_SEPARATOR, and then followed by its value.0899

Here, instead of having a constant defined in config.php that is called ITEMS_DATA_SEPARATOR,0905

we just define a data separator function within an Item class.0913

And then, when we reference it, we reference it using the scope resolution operator.0916

And the way you do that is: in this case, we have not used the self method; we have just done the class name, which is another way of doing things.0929

You have the class name, followed by the scope resolution operator, followed by the name of the constant that we are getting.0941

And so, that is how we are able to access that class constant.0947

We have done the same thing for our Department object.0951

If we look at the source code for it, the Department object had two DATA_SEPARATOR constants.0955

One was to separate each of the data fields; and then, as we know, 0961

all of the item ID's in the department are separated by commas, so we have an itemID separator.0964

We have created constants, moved them out of config.php, and moved them into the Department class;0969

and then, we have also created this static buildObject function that takes in a Department string0975

that was created from the __toString method of the Department object.0981

We explode that string, based on the data separator.0985

We go ahead and start building up the Department object, and then we explode that third field, which is the comma-delimited list of item ID's.0989

And then, we add each item to the object, and we return the department.0998

What we have done is taken that method that was in our function library and moved it into our class library.1001

And in a way, it kind of makes sense, because it is something that is related to a Department object.1009

And one of the points of having classes to locate common functionality related to a common theme or subject into one area1014

(in this case, our Department class): if we have a function 1024

that is going to build a Department object, it makes sense to have it in the Department class.1026

In this case, we make it static so that you don't have to have an instance of the class created,1032

because all we want to do is: we already have a Department string, and we just want to build an object from that.1037

So, we don't need to access instance variables, so we have created the static method.1042

If we go and take a look, there are a couple of different methods where we have made use of this new function; and we will take a look at some of those.1047

For example, if we go to itemExists, what you can see is: what we have done is:1054

in this function, we loop over each line of the file, and we test to see if the itemID in each line is the itemID that we are looking for.1065

And this method takes a string that is an item ID that you are searching for.1073

What we do now is: we read a line from the file, and then we call this static method buildObject on Item, and we pass it the line.1078

Now, that line represents an Item object string.1086

As long as that Item object string is well-formed, meaning it comes from the __toString method of the Item, this will return OK.1089

And what that is going to do is: it will create that new item, and then we can access the itemID in that Item object we have built up,1097

compare it to the itemID that we are looking for, and if it is the right one, then we can break out of the loop and say,1104

"OK, the item has been found; exist equals true."1108

Then, the same thing for departmentExists...1112

We have also used it, for example, in getDepartment, getDepartments, getItem, and getItems.1115

For example, in getDepartment, we are extracting a line from departments.txt, and we are building a Department object out of it and returning that.1125

What we do is: we read each line from the departments.txt file.1135

We go ahead and call our new static method buildObject; we pass it the line and the file,1140

which is a string representation of a Department object.1145

This static method will build a Department object out of it; it will return it into this department variable.1148

We test to see if the ID of that department is the ID that we are looking for; and if so, it has been found.1155

And then, we can just simply return this department variable, because that contains the object that was built by buildObject.1162

And so, that is how these getDepartment, getItem, getDepartments, and getItems methods work:1169

by using these new static methods that we have incorporated into the class.1177

We have taken buildItemObject and buildDepartmentObject, moved them into their respective classes,1181

renamed them buildObject (and the reason we are not calling it buildDepartmentObject is: the way you would call it1187

would be Department::buildDepartmentObject, and it is kind of redundant, so we are just saying Department::buildObject),1194

and then we are able to pass it a Department string.1200

This buildObject is the analog of the __toString method we created in a previous version of the web application.1202

And in that case, we used to have a createItemDataString, createDeptDataString--we moved that into the class and made that into the __toString method.1208

So, we are taking a common functionality related to a department and related to an item, and focusing it,1217

and putting it all in that one class, so that it is in a related spot.1221

And so, what this shows us is an example of a static method, because this buildObject is declared as a static method.1229

And it shows how we have, for example, our Department class and our Item class:1238

unlike the Output class that only has static methods in it, our Item class and our Department class have instance methods.1243

They have instance properties.1248

Well, now we have added constants to them, and then we have added the static method buildObject.1250

So, this shows how we can have an object that has instance methods and instance properties, and also can have a static method to be called.1254

That ends today's lesson; thank you for watching look forward to seeing you next time.1262