Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
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

Lecture Comments (1)

0 answers

Post by dave smith on August 15, 2015

In the homework solution, it would be better to add [] to the $header property like this: class HttpRequest{

public $uri;

public $headers = [];

rather than this: class HttpRequest{

public $uri;

public $headers;

That way you sure to not get an error even if you call the addHeader() method and pass in no arguments.

Object Methods

  • Classes can define functions, known as object methods, that instances of the class can execute.
  • An object method is defined within the curly braces of a class definition using the same syntax used when creating user-defined functions.
  • Object methods are called using the arrow operator ->. An object variable is followed by ->, the name of the method, and a set of parentheses, which can optionally include any arguments to be passed to the object method:
    $person1 = new Person();
    $person1->hello(); // object method requiring no arguments
    $person1->helloWithArg($arg1); // object method requiring arguments
  • Object methods can return values just like user-defined functions.
  • $this is a special variable in PHP that is used within a class’s methods to refer to the current instance of a class.
  • A getter method retrieves the value of an object’s property.
  • A setter method sets the value of an object’s property.
  • An object method’s return value can be output within double-quoted strings using curly brace syntax:
    $person1 = new Person();
    echo “First: {$person1->getFirstName()}”;
  • Object methods can call other object methods of the same object instance by using the $this variable.

Object Methods

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:12
    • Lesson Overview
  • Object Methods 0:52
    • Definition of Object Methods
  • Calling Object Methods 3:25
    • Calling Object Methods
    • Coding Example: Simple Object Methods
    • Object Methods and Return Values
  • $this Variable 10:06
    • $this Variable: Definition and Example
  • $this Variable (cont.) 15:51
    • $this Variable (cont.)
  • Getters & Setters 21:21
    • Getters & Setters Methods
  • Object Methods In Strings 25:46
    • Object Methods In Strings
    • Coding Example: Outputting Method Return Values in Strings
  • Using $this to Call Other Methods 28:49
    • Using $this to Call Other Methods
  • Homework Challenge 34:18
    • Homework Challenge: 1-3
  • Homework Challenge (cont.) 36:20
    • Homework Challenge: 4-6
  • Homework Challenge (cont.) 37:52
    • Homework Challenge: 7-10

Transcription: Object Methods

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

In today's lesson, we are going to be expanding our knowledge of object-oriented programming by learning about object methods.0005

Specifically, we are going to talk about what object methods are and what their purposes are.0014

We are going to talk about the subject of how to call object methods--the syntax that is used to do that.0019

We are going to talk about a special variable, known as the this variable, which is going to be important for use within our object methods.0025

We are going to briefly talk about what are known as getter and setter methods.0033

And then, we are going to finish up by talking about how to output the results of object methods within double-quoted strings,0037

and how to use this this variable we are going to learn about to be able to call one object method from within another object method.0044

Classes can define functions.0055

In a way, we have been having our own function libraries.0058

For example, fileLIB.php has been a library that just contains a number of function definitions that we have been able to use.0060

Well, when you define a class (for example, the Item class), you can define functions.0067

And functions that are part of a class definition are known as methods.0075

And they are functions that work on a particular object.0081

So, when you define an instance of the Item class (for example), it might have a method that says returnCost.0087

And when you call that method on that particular item, it will return maybe the price property of that particular item.0095

The object methods are defined...and it is still a function, but again, the terminology with objects is to call them methods...0104

they are defined within the class definition, just like a class's properties.0112

And it is defined within the curly braces; we are going to take a look at what it looks like in a second.0116

It has the same syntax that we use for defining functions, in that it even uses the function keyword.0122

The one difference is that, at least for the time being, we are going to be placing the public keyword in front of the function keyword.0127

Let's take a look at a class Person where we have defined a class method.0137

For example, we have a Person class with two properties, firstName and lastName,0146

which would just be strings to represent the first name and last name of the person.0152

And this is what we have seen before; we have come across this when we first learned about objects, about how to define properties.0155

But also, if you look down here, you can see that we have defined a function.0161

It is within the curly braces of the class definition, which means that it is a method of this Person class.0165

And we use the keyword public, and then function; we say public function,0171

and then we define the method just as we would any other function that we have done before.0176

In this case, we define a method called hello that takes 0 arguments; it doesn't return anything; it just outputs an echo statement.0181

All the rules that apply to functions that we had learned about in the introductory course,0193

that you know about from your experience with PHP, are going to apply to object methods, as well.0198

We know that we can define methods and objects; but how do we call them?0206

Well, first of all, you have to have an instance of an object to call the method.0210

And then, in order to call the method, you use the same accessor syntax that we use to access the values of the different properties.0214

And you use that to call an object's method.0222

For example, let's say we define a variable called person1, and we set it equal to a new instance of this Person class that we have talked about.0226

Well, in order to call the hello method on that Person, we list the name of the variable, followed by the dash and the greater than sign.0238

And then, instead of following that by the property name, we follow it by the name of the function.0247

In this case, we had a function called hello.0251

And the way that you distinguish it as a function, as opposed to a property, is: you include a pair of parentheses afterwards,0253

which is sort of intuitive, because that is the syntax we use for functions.0262

Now, for example, if our hello method of our Person class took a parameter, then we would also specify the parameters0266

within the parentheses, just as you would any time we have been calling any other functions that take parameters.0277

Let's take a look at our Person class; we defined this function hello--let's take a look0285

at a file called hello.php that is going to call that method, so we can see this in action.0291

We have a test file here that includes the definition of the Person class.0297

We instantiate, or create a new instance of, a Person object.0302

We go ahead and use the accessor syntax to set the firstName and lastName properties of this particular Person object, this instance of a Person object.0307

And then, we are going to call, or invoke, the hello method for this particular Person object.0317

In this case, we use the dash, greater than sign, and then we say hello with parentheses.0324

If we just quickly look back, all our hello function does is outputs or echoes a statement that says 'hello.'0330

If we go and look at this lecture example, and we look at hello.php, we can see that the output of it is A person says 'Hello!'0338

And that is a result of calling this method on this Person object.0347

And so, that is the syntax that you use to invoke an object's method.0354

One other thing to do is (for example): we could, just as with other functions, include parameters.0362

So, let's include one parameter--it's a string called world.0368

Now, we could go to our Person class, modify the function definition so that it accepts a string, and then say,0375

"We are going to do the same thing; we are going to have an echo statement, but we are going to echo the string that was passed in."0386

So now, we have an object method that can take a parameter, as well.0398

So, if we go ahead and (both of these are saved) reload our page (and let's make this string that we are passing in...0403

OK, it is world, so it will be different), we can say, "A person says 'world.'0409

So there, you can see how an object method works just like a regular function does.0416

One other thing to note is that object methods can return values, just like a normal function can.0424

For example, let's say that we had an object that modeled a shopping cart (which is something we are actually going to be adding to our web application).0429

Let's say we already have that, and we have declared an instance of it.0435

So, we have this cart1 variable that is an object of our ShoppingCart class.0438

Maybe that ShoppingCart class has a method on it called calcCartTotal, which is a function we have been using in our utility library.0443

Well, if you run that calcCartTotal function on a particular cart object, let's say it calculates that cart total and returns it as a value.0453

So here, what you do in order to get the return value of the object method is: you call it just like you would a regular function.0459

And you can use an assignment statement; so in this case, we are assigning the value returned from the calcCartTotal function into the total variable.0469

And that is the same thing as if we had maybe, instead, done total= calcCartTotal, or maybe we passed it an array of the items in the cart.0481

Whereas this would return the value--the return value of this function takes its place, and total gets set to it;0503

the same thing happens here--the return value of this whole object statement gets returned and gets set into the value total.0509

For example, we could alter our hello method of our Person class,0520

so that instead of echoing the string, we could have it return a particular string.0531

For example, get rid of the echo statement and just have it return a static string that says, "A person says 'Hello!'"0536

So now, what we can do is: if we run this the way it is, what is going to happen is:0549

it is going to call this hello method; it is going to return a value; but we are not echoing it, and it is not echoing it in the function.0554

So, if we run this script again, nothing is going to happen; no output is going to happen for that function.0563

So, what we do is: we update it to just echo the result of that.0568

Here is an example of having an instance of an object, calling a method on it that returns a value,0574

and then using that value--using that return value, just as you would a return value from any other function.0582

Here, we are going to echo the value returned by this hello method of the Person object, which is going to be that string.0587

And if we refresh the page now, we can see that it goes ahead and outputs it.0595

That is an example of how object methods can also return values, as well.0600

Now, that particular function didn't do too much; it especially didn't do anything related to the object.0608

It just output a simple string: you could just create a regular function to do that.0615

Typically, the reason you create object methods is so that they can perform some actions on that particular object.0618

One of the main things that they can do is that the functions defined within an object can access the members,0625

or the properties, of that particular object--and actually even the other methods of a particular object.0632

And the way that you do that is: there is a special variable called this: it's $this, and you can use it within class methods.0637

And what it does is refers to a current instance of an object.0646

And it is kind of an abstract concept, so let's take a look at what it looks like in code, so that it will make a little more sense.0650

Let's say we have our Person object, and we have our hello function.0659

Now, we have created a new method; you can have as many methods for a class as you want.0662

We are calling it getFirstName, and we just want it to return the first name of this Person object.0668

Now, we could access that using our typical accessor syntax; but for the sake of this lesson, and learning the material, we are going to do it through the method.0675

And what you do is: in order to access the firstName property of this particular Person object,0685

you use the this variable, and then you use the accessor syntax, followed by the property name that you want to get.0695

For example, if in our test script this.php, we define two Person objects.0703

We set both the firstName and lastName of each of the different objects.0715

And then, we are going to call this getFirstName method that we have defined on each person.0720

For example, person1 is defined...we set the firstName equal to Joe, and lastName equal to Smith.0726

When we call this getFirstName function, what it is going to do is invoke this getFirstName function.0733

And it is going to return the value of the property firstName of the this variable.0740

Now, the this variable changes for each particular object instance; so each object instance...0749

every time you call this getFirstName function, this is reassigned a new value.0755

So, when we call this function on our person1 object, this is going to refer to person1.0760

In a way, it is as if we replace it with person1/Person.0768

But the way you do that within a class definition is: you just use $this.0775

Now, the reason for doing that is that now we can reuse that.0779

If I go back and type it again...if we had person1 here, now this function would only work with a variable named person1.0783

By using this special variable $this that can change its value, depending on which instance of an object was being used,0789

we can reuse this method, and so it is going to work for every object we create of this type.0797

For example, person1--we had set their firstName equal to Joe, so when we call getFirstName on person1,0807

this in that method is going to refer to person1, and it is going to get its firstName property, which is Joe.0814

Now, when we go down here on person2, and we call getFirstName, that this variable, when that method gets run,0821

is going to refer to this person2 object, whose first name is Jane, and it is going to return that property.0827

And so, what that does is: it is the same method that gets run, and has the same code, but it returns different values,0836

because each time that method is called, that this variable gets reset to the variable of the object that you are working on.0842

For example, when we run this script, what it is going to output is the name of both of our different people.0855

So, if we run this.php, we can see that the Person firstNames are Joe and Jane.0865

If we look at our code, we can see that we just had two echo statements0869

that called the getFirstName method on person1 and getFirstName on person2.0876

So, you can see that each of those methods returned a different value.0880

It returned the firstName value for that particular instance of that object, because0885

when the method is called, that this variable changes to become the instance of the object you are operating on.0890

Now, one thing to notice is that, for example (and this is a required thing):0898

let's say, in this function, we wanted to access this firstName property.0902

Well, you might think that you could just access it using this $firstName, because you would say, "Hey, we have this class;0906

it has the firstName variable defined in it; let's just return firstName."0913

If we go ahead and try and do this, and we run our script, we are going to get an error saying that that variable, firstName, is undefined.0920

So, it is very important that when you want to access the properties of a class within a method, you use the $this keyword.0929

So now, when we set it back to what it was before, it is going to work.0943

So again, because this $this variable is kind of an abstract concept, I want to draw a little diagram to explain it a little bit better.0952

For example, let's say we have a class that we have defined called X.0960

It has one property in it, and we'll just call it var1.0966

And it has one method that is just going to return the value of the var1 class property.0976

And it is going to use the this special variable.0995

What this essentially does is: when we define a class, we are defining a template.1012

We are saying, "This is a template for any variables that we declare of this data type (of this class, or this user-defined data type)."1015

So, we are saying that any time we define a variable that is of the class X,1024

it is going to have a property var1, and it is going to be able to execute this var1 function.1029

And so, it is code that defines a data type and defines methods that can be run on that particular data type.1035

And then, it can be used in multiple spots.1043

So, just as in our other application, we are going to define two instances of this X class and set var1 equal to 'A'...and 'B'...1045

Now, again, what is going to happen is: as this class is a template, when you create this new instance of the class X1080

and assign it to variable 1, this is saying that variable x1 is going to have this template assigned up here.1091

So, it is going to have a var1 variable; so when we assign var1 to it, it gets its own copy of that var1 variable.1098

So, each instance of a class (and this is an important concept, which is why I'm harping on it a little bit--it's that)...1104

every time you create a new instance of a class, you are creating a whole copy of this class here, in a new way, for that particular variable.1113

So, we are creating a whole copy of the var1 property, and we can set it to value A.1122

Now, when we create this x2 variable that is another instance of that class X,1128

we are creating another whole copy of that class, which includes its own version of var1.1134

Oops, I made a mistake down here.1139

When we set x2 var1 equal to B, x2's var1 property is completely separate from x1's var1 property; it is its own instance.1142

In the same way, for example, if we were to call this getVar1 method on our x1 object, it is calling its own version of that particular method.1153

And the way it does that is using this $this keyword.1172

As mentioned before, what happens when you call this getVar1 method is: this function up here gets replaced with...1175

I'll just use a shortened syntax without the public keyword...1185

So again, it is as if, when x1 calls this object method, it calls that method on itself--on its own instance.1210

So, what it does is: when this method gets run, this gets replaced, essentially, with x1.1218

It is just as if we were accessing that var1 property of x1 directly.1224

Now, when x2 calls this getVar function, this is going to get replaced with x2.1229

What this is reinforcing is this concept that the class provides a template of the properties and methods that a particular class can have.1238

And when you instantiate, or create a new instance of, each class,1249

you get copies of each of the different properties, and a new var1 property for each instance.1254

And you also get your own version of the getVar function.1261

And the way that you do that is using this $this method, which PHP is able to dynamically interpret when you call that function.1264

So now, we can define this getVar1 function one time, and it is going to work on any time we have an object of the class X.1272

One common operation performed on objects, a common method that is performed, is to get and set their property values.1284

We just saw an example, in our Person object, of something that is known as a getter.1289

It returned the value of our firstName property; it was called getFirstName.1294

So, getter methods retrieve the value of an object's property, and its setter methods set the value of an object property.1298

Now, we have been able to do this: let's say we had our person1 from before--we can set its firstName using this syntax here.1307

So, we actually don't need these getter and setter methods.1317

But it is a common thing that is done, and it is a special topic within object-oriented programming,1324

and within PHP, that we are going to get to in another lesson.1330

So, I just wanted to at least mention it--at least introduce the concept of it.1333

Now, we can set the firstName property of this person1 using this syntax that we use, the object accessor syntax.1339

But instead, we could create a setter method and say person1 (let's say we set firstName equal to Joe)...1350

we could define a method called setFirstName that passes it the value Joe.1365

And what that is going to do is: if this is a setter, it is going to take this value Joe,1374

and then, within the class definition, it is going to set the value of the firstName property by using the this variable.1379

We are going to see that in a second.1387

And one of the main reasons I am demonstrating this is that it shows how you can use the this variable1389

to not only return values from an object, and to access property values of an object, but also to be able to set them.1394

For example, if we look at a new version of our Person class, we have the same two properties: a firstName and lastName.1402

And then, we have defined what are known as getter functions for each of the properties.1411

We defined getFirstName and getLastName, and what they do is use the this special variable1415

to return the first name and last name, respectively, of this particular instance of a Person object.1420

And we also have setter functions, which take a parameter.1429

Again, object methods can take parameters, just as any other function does.1431

And then, here, we are using the this variable, and we are using the accessor syntax, to set the value of a particular property.1435

In this case, when this function is called on an object, we are saying, "On that instance of that object,1444

we are going to have the firstName property, and we are going to1451

set it equal to this firstName variable, which is passed in here."1453

This is how you can use the this function in order to also set object properties.1456

If we go and look at a test script, gettersSetters, we can see that we create a new Person object.1467

And now, instead of using the method that we had done before to set the first name and last name of the Person1473

(which uses this accessor syntax), we are going to use our setter methods.1481

We are saying person1: set firstName Joe; set lastName Smith; the same thing down here...1485

And it is going to perform the exact same function as when we had set them manually.1491

And when we go to output the person's first and last name, we are going to use the getters getFirstName and getLastName on each object.1494

What they are going to do is show that those values we set with the setter methods actually set those methods for those particular objects.1502

When we load this script up, we can see that it says Person #1's first name is Joe;1509

Person #1's last name is Smith; and the same for Jane Jones, who is Person #2.1520

And so, what you can see is that we have been able to set object properties within an object method (and that uses the this function).1527

And then, we are also able to access those properties, using the this variable.1536

And we do that using our getter methods, down here.1540

One thing I want to talk about now is: object methods are functions, and they return values.1548

And before, we have had strings where we have the value of a function that we want to output.1554

And you can't include that within double-quoted strings--you can't call a function within double-quoted strings.1562

However, you can include the output of object methods within double-quoted strings.1567

And the way you do that is using the curly brace syntax.1573

So, let's say we define a variable person1; we are going to define it as a new instance of the Person class.1576

We are going to use that setter we defined--we are going to set its firstName to Joe.1583

And then, we want to output the first name.1586

Well, what we can do is: if we want to output the value that getFirstName method on person1 object returns,1589

we simply call the method, using our normal method-calling syntax, and just enclose it within curly braces within our double quotes.1596

And this is opposed to having to use the concatenation operator (and I'll abbreviate).1604

It is a nice little trick that makes our code easier to work with: now, we can use methods within our string.1626

And that is something that...let's say this wasn't an object; normally, for a function, we couldn't do that anyway.1638

So, if we had a function called getFirstName (not an object method, just a function),1644

we wouldn't be able to include that, normally, within double-quoted strings anyway.1655

Let's take a look at this in action: we have our same Person object that we had in our last example.1662

It has the getters and setters for the first and last name.1668

We go ahead and instantiate two Person objects and set them with the same names we did before, Joe Smith and Jane Jones.1672

And now, what we are going to do is: whereas before we output their names using this concatenation operator1680

(we said Person, First Name, the dot operator, and then we called the method getFirstName),1685

now we know we can do it all within one string.1692

And so, instead of (actually, let's move it back again) having three separate strings here that we are concatenating together,1695

we can include it all in one; and you do that using this curly brace syntax.1701

This does the same thing as the last script does, except we are making use of including the outputs of an object method within a double-quoted string.1707

If we look at what it looks like when we browse to it, it is going to look exactly the same; and you can see that here.1717

One final topic that I want to talk about is using the $this can also use that to call other methods from within object methods.1730

For example, when we were in our (let's take a look at our Person class) getLastName,1742

we could use $this to refer to the particular instance of the object...1751

Well, when this method runs, this gets set to whatever instance of that object you are running the method on.1756

And we use this to access that lastName property of that Person object.1763

We can also use $this to call object methods from within itself.1770

For example, we have our function hello down here; before, if we look at the very first version of it, it just returned a string.1776

Well, now what we are doing is calling our function hello; and within our function hello, we are going to say, "Joe says hi" or "Jane says hi."1791

And the way we are going to do that is by making use of this getter function we defined for this Person class.1800

And we are going to call it within this object method.1806

And the way we do that is using this variable again.1808

When we create a variable, a Person variable, and we set its first and last name, and then we call the hello method (for example)1812

on person1, what is going to happen is: hello is going to get called; $this is going to get replaced with the person1 object.1823

And then, it is going to say, "Call the getFirstName method on it."1831

Well, getFirstName is defined within the class; it will go up here, and it will say, "return $this-->firstName."1835

So then, $this will also know that it refers to Person object 1, because PHP does that for you automatically.1840

It is going to get the firstName property of it and then return it.1848

So then, it is going to put that name here, and then concatenate it to this "so-and-so says hello."1851

This is an example of how you can use the this variable to also call object methods from within other object methods.1857

You can access object properties using the this variable, and you can also call other object methods within them, as well.1866

If we run this script, we can see that now we get an output message that says "Joe says hello" or "Jane says hello."1873

When this ran, and this function is called, we are calling on this object the getFirstName method.1886

So, on person1, we are calling getFirstName; when we go to getFirstName,1894

we are returning the firstName property of the person1 object that is replaced by this.1899

And then, that is how that gets output.1905

Now, this isn't a particularly real-world example; something that is a little more real-world is maybe...1907

because we are typically not going to be using getter and setter functions, as you will see in a future lesson...1914

something that we might do is: maybe we will define a function called getFullName.1919

Because fullName is not a property of a Person object, you can't access it using the accessor syntax.1923

For example, let's just say this was in a random script: you couldn't say fullName, because fullName is not a property of person1.1932

What we can do, though, is create a useful method that is going to return the full name.1946

And what that is going to do is concatenate the first name and the last name, which are properties of that object, and return that.1949

And that might be a useful function: we are going to say, "Give me the full name of this person."1955

If we define an object method in our Person class called getFullName, it will return a string.1960

And this is using our curly brace syntax, again, to output...well, in this case, it is outputting object properties within a string.1966

We are saying, "When you call getFullName, you are going to return a string that is going to contain1973

the value of the firstName property of this particular object, followed by a space, followed by the lastName property of this object."1981

And so now, in our function hello, we can make use of this function.1990

You can make use of it externally, but we are going to make use of it internally here,1996

to again reinforce the idea that you can call object methods within other object methods.1999

We are going to update our hello function to now call $this-->getFullName.2004

So, when hello gets called, it is going to say, "On this object, call the getFullName method."2009

And then, it is going to return a string that has the first name and last name (concatenated together) of this particular object.2016

And then, we are going to concatenate that 'says hello,' and so hello is going to2025

return a string that is going to say 'Joe Smith says hello' or 'Jane Jones says hello.'2031

If we look at the output of this script, we can see 'Joe Smith says hello'; 'Jane Jones says hello.'2036

That is just a combination of all the different things that we have learned,2045

and something that is maybe a more practical use of how you would see these object methods used.2048

For today's homework challenge, it is kind of an advanced homework challenge.2060

I'm going to have you create a class called HttpRequest that is going to represent an HTTP GET request message.2063

And that is something we learned about in an earlier lesson in the course.2071

I want this HttpRequest class to have two properties: one called uri, which is a string2076

(and that is going to represent the particular URI that this particular requested object would be requesting),2082

and then a headers property, which is an associative array that is going to contain2088

name/value pairs of any HTTP headers for any particular HTTP request this HttpRequest object would be representing.2094

And then, now that we have our properties, I want you to add a method to the class called addHeader.2102

It takes two parameters (in this case, they are called name and value),2108

where the name represents the name of a particular HTTP header, and the value represents the value of the header.2113

So, for example, the name might be host (which is an HTTP header), and the value might be

What this addHeader method is going to need to do is: it is going to need2130

to be able to access this headers property of the HttpRequest object in order to add this name/value pair to it.2134

And so, in order to do that--to access, from within an object method, the properties of that object--we need to use the this variable.2142

So, you are going to have to use the this variable within this method here.2149

Then, I want you to add a method called getHeaders; and what it is going to do is return a string in proper HTTP request format2153

that represents all of the headers that are in this headers array up here.2163

So again, you are going to have to use the this variable to access this headers array.2166

And what you are going to do is loop over the array, and then output each header on its own line, with the name and value separated by a semicolon.2170

And so, that is going to be a method that is going to be used internally within the function.2178

Finally, I want you to define a method called getRequest.2182

And this is the method that is going to be used by people that are using this class.2186

They are going to instantiate an HttpRequest object; they are going to set its URI; they are going to maybe add some headers using the addHeaders function.2190

And then, they are going to call the getRequest method, which is going to say,2198

"Give me the formatted HTTP request that is represented by the URI and headers of this particular object."2201

What getRequest is going to do is create the required first line of an HTTP request,2210

which has the method name (in this case, it is going to be get) of the URI and the HTTP version.2219

And then, it is going to call the getHeaders method to append all of the HTTP headers to this first line that it creates.2224

This is an example of an object method calling another object method.2233

In this case, getRequest is going to be calling the object method getHeaders.2237

Then, just in a separate script, I want you to instantiate an HttpRequest object.2242

Set the uri property to index.html (and you can just use the object accessor syntax).2248

And then, add (using the addHeader method) two HTTP headers: one called Host, and one called User-Agent (which are both real HTTP headers).2255

You can set them to whatever value you want--it is not as important for the example,2264

but just the fact that you are going to be practicing using object methods.2269

And then, call the getRequest method on that object and output what it returns between pre tags,2274

which is going to show the output formatted in this form down here, which is like a computer text format.2281

And then, verify that your output has the form shown here.2290

For example, this first line of yours should be exactly the same as this: it should say GET /index.html HTTP/1.1.2293

And then, it should have two header lines, one for Host and one for User-Agent.2302

And their values should be set to whatever values you supply to that addHeaders method.2305

Notice that they are formatted properly with a colon and a space in between here.2310

And you are going to be doing that in your getHeaders method.2315

And so, for this, you need to make sure that you are appropriately using the this variable2321

to call, for example, the getHeaders method from within the getRequest method,2327

and also to be able to access that headers variable and the uri variable.2333

For example, when we are outputting this first line in our getRequest method, we are going to have to have access to the uri property of this class.2337

So, as a little hint, one of the things that you are going to need to do is use the $this-->uri in order to access that.2346

So, just make sure that you properly use the this variable, and that, when calling the methods,2355

you also practice using that same accessor syntax that we used to call methods.2361

For example, let's say we had an object of HttpRequest called http; to call the getRequest method on it,2366

as you will remember, we use this dash, greater than sign, the name of the method, followed by parentheses.2382

In this case, getRequest doesn't take any parameters, so it will just be empty parentheses.2387

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