Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Advanced PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

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!

Classes vs. Objects

  • A class provides a definition for a user-defined data type. An object is an instance of a class providing values to a class’s properties.
  • Each instance of a class has their own ‘version’ of a class’s properties, and these are referred to as instance properties.
  • Static properties are class properties whose values are shared between all of the instances of a particular class.
  • A property is declared static in a class definition by using the static keyword:
    public static $numPeople = 0;
  • An object of a class does NOT need to be instantiated to access a class's static properties.
  • The scope resolution operator (::) is used to access static properties.
  • A public static property is accessed from outside of a class definition using the syntax:
    Person::$numPeople; // uses the class name
  • A static property, whether public or private, can be accessed inside a class definition with an alternative syntax that uses the self keyword:
  • A static property CANNOT be accessed via an object instance variable using the -> operator.
  • Static methods are methods defined in a class that are at the class-level and are NOT associated with particular instances of a class. They are declared with the static keyword:
    public static function add($op1, $op2) {…
  • Static methods CANNOT use the $this variable within their definitions.
  • Non-static methods are referred to as instance methods.
  • Static methods are called using the scope resolution operator (externally using the class name, and internally using the self keyword):
    • Externally: Person::getClassName()
    • Internally: self::getClassName()
  • A class constant is a constant defined within a class definition that is specific to that class. They are defined using the const keyword:
    const PI = 3.14;
  • They are accessed using the scope resolution operator (with no $ prepending their name):
    • Externally: Math::PI
    • Internally: self::PI
  • Additional Resources:

Classes vs. Objects

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:09
    • Lesson Overview
  • Class vs. Object Review 0:58
    • Class vs. Object Review
  • Static Properties 2:42
    • Static Properties
  • Scope Resolution Operator (::) 5:26
    • Scope Resolution Operator (::) & Static Property
    • Coding Example: Accessing a Static Property Using the Scope Resolution Operator
    • Coding Example: Accessing a Static Property in a Class Definition
    • Coding Example: Using a Static Property as an Instance Counter
  • Static Methods 18:51
    • Static Methods Overview
    • Coding Example: Static Methods
  • Classes vs. Objects 26:28
    • Classes vs. Objects: Diagrams
  • Class Constants 30:24
    • Class Constants Definition
    • Coding Example: Using Class Constants
  • Homework Challenge 34:01
    • Homework Challenge: 1 - 5
  • Homework Challenge (cont.) 35:25
    • Homework Challenge: 6 - 10

Transcription: Classes vs. Objects

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

In today's lesson, we are going to be continuing discussing the topic of classes versus objects.0004

We are going to have a review of what classes and objects are, and the differences between the two,0011

because that is going to become important when we learn about what are known as static properties and static methods.0016

So far, what we have been working with (as you will find out) have been known as instance properties and instance methods.0023

They are properties of a class and methods of a class that relate to a specific instance of that class.0029

We are going to learn about something a little different; it is on more of a class level.0035

We are going to talk about an operator called the scope resolution operator, 0040

which is going to allow us to access these static properties and methods that we are going to learn about.0043

And then, we are also going to learn about constants in classes.0047

We know about being able to define constants in our regular PHP files; we are going to learn about how to define them at the class level.0050

A class, as we know, is essentially a user-defined data type.0061

And when you create an object, what you are doing is creating an instance of that class.0065

You are instantiating a copy of that class, and you are filling in the values for the properties of it.0069

Essentially, what you have between a class and objects is a one-to-many relationship.0077

You have one class definition, and you can have as many different object instances of that class as you need.0081

The class provides the template, and the objects are the realization of that class.0088

But in the case of each instance--each instance is essentially (as we had mentioned) its own version of that class.0094

So, they have their own version of all of the properties.0112

For example, we have learned about object properties.0115

Object properties defined in a class definition get initialized and set when you create or instantiate an object.0116

Those properties are specific to that particular instance, so each instance of an object has its own copy.0119

For example, if a Person object had the firstName property, each Person object you create 0123

would have its own copy of the firstName property, and it would be specific to that instance.0130

And so, for that reason (we haven't specifically called them that, but the distinction is going to become clear today), those are known as instance properties.0134

So, all the properties that we have been dealing with so far with our classes are known as instance properties.0141

They are properties that are specific to each instance of a class.0146

Each time you instantiate a class as an object, it gets its own version of those properties; and they will be referred to as instance properties.0150

Static properties are what we are going to talk about today.0164

Static properties are, in a way, like class properties; they are properties whose values are shared between all of the instances of a particular class.0167

So, whereas in your class definition--let's say, in our Person class--we have a firstName property;0181

well, that is an instance variable, if we define it as we have been...0188

And that means that it is an instance property, meaning that each instance of that class has its own copy of that property.0192

However, a static property is per class, meaning that there is one property defined in that class definition0202

that all of the different instances have access to, but it is not like they get their own individual copy.0209

So, if one instance of a class updates this static property of the class, it affects all of the other instances, in that they all read that same value.0215

The way you declare a static property in a class definition is using the static keyword.0227

Whereas before, we had just been saying public numPeople, and that would be our property definition;0233

now, we are added the static keyword: we are saying public static numPeople, and we are giving it an additional value of 0.0239

What this statement says is that we are declaring a public property that is a static property.0245

It is going to be called numPeople, and its initial value is 0.0250

And this would be contained within a class definition, just with all your other properties.0255

One other thing to note about static properties is: because they are not specific to an instance of a class--0260

they relate to the entire class, or all of the different instances of that class--0267

you don't have to have an instantiated object in order to access them.0273

Normally, for example, let's say we have a Person class, and it had a property firstName.0277

Well, to access that firstName property, we would write something like this.0290

And what this is: it is a reference to this instance of the Person object that was created.0298

Well, that is why it is called an instance variable, because this copy of firstName is specific to this a variable right here.0304

Well, because static properties aren't specific to a particular instance, you don't reference them using an object variable.0315

And we are going to see about that in the next slide--about how you actually access them.0323

What PHP provides is the scope resolution operator, which is two colons, one right after the other.0328

And that is how you access static properties.0334

For example, if we had a Person class where we defined a static property called numPeople0337

(just as in the last slide, public static numPeople equals 0), well, in order to access the value of that static variable outside of the class,0342

the way you access it is: you write the name of the class, followed by the scope resolution operator,0353

followed by the name of the variable--including the dollar sign.0359

And so, outside of a class, this would be how you reference numPeople.0364

And the reason you can access it: it is essentially the same thing as a class property,0369

because numPeople is a property of this Person class, because there is only one version of it.0375

There is not a version for each instance of the Person class.0378

And you can do that for public static properties.0382

In addition, if you are trying to access a static property from within a class method 0385

(for example, if you have a class method defined within the Person class, 0390

and you want to access the static property), you do so using this keyword self.0394

If this was, for example, a function or a method within a class definition, in order to access the numPeople static property of this class,0400

of this object, you would say self, and then you would have two colons, followed by the variable name.0411

And again, you include the dollar sign, which is a little different, because when we access instance variables,0416

and we use this arrow operator, and if we had a property called numPeople, we access it like this, without the dollar sign.0425

So, that is one definite distinction between accessing static properties versus instance properties.0436

And then, another thing to note is: outside of a class, you can't access a static property using the arrow operator.0445

So, for example, if we had this numPeople static property defined in a Person class,0452

we would not be able to access it using this arrow syntax.0459

Assuming a was an instance of a Person object, we couldn't use this arrow operator in order to be able to access this numPeople static property.0462

The way you do it is: you have to have the name of the class with the scope resolution operator.0474

Let's take a look at a basic example of static properties in action.0477

We have this Person class that we have defined.0484

It has two private instance properties, and these are firstName and lastName.0487

And they are particular to every instance of the Person class that gets instantiated.0490

So, every Person object that is created gets their own copy of these.0495

We have a constructor that takes two parameters: one is the firstName and the lastName that you are going to set for the Person.0498

Here, we set these instance properties.0504

And then, we are just also outputting a message and a constructor, just to say we are creating a Person (firstName and lastName).0510

Now, up here, what we have is a static variable.0515

You can see here, with the keyword static, we say public static; we are calling the static property for this Person class description.0518

And it is a description of the class; it says, "This class represents a person."0526

It kind of makes sense that, for any Person class that you have out there, if you want to get a description of it,0530

they all might have the same description: "This represents a person."0535

So, this may not be the most practical example, but it is used to demonstrate the use of a static property.0539

I'll show a little more practical example in a couple of examples.0546

If we look at a script called staticProperties.php, we have an include statement that includes that Person class that we just looked at.0553

And then, what we have (let me bring this down to another line) is just a message that says, 0562

"I am outputting this static description property of the Person class."0569

And the way we access that is by the name of the class, the scope resolution operator (which is two colons),0578

followed by the name of the static property, including the dollar sign.0585

When we echo here, we are saying, "The value of Person::description equals" and then we are outputting it using this concatenation operation.0591

If we load this script up, and we take a look at staticProperties.php, we can see (let me blow it up a little bit):0601

Person::description--the value of it equals, "This class represents a person."0610

And if we go back and look at our Person class, we can see that that is the value that we had assigned to this static property.0616

That is how, outside of a class, you access a static property.0625

You use the class name, the resolution operator, and then the name of the property.0629

We had also talked about being able to access it from within a class.0635

So now, we have the same class here, except we have added what we are going to call (and we are going to learn about it in a minute) an instance method.0639

Up to this point, we have just been referring to it as an object method called getDescription.0651

And all it does is: it just returns the value of this class's static property description.0655

And as we mentioned in the slide, when we are trying to access a static property from within an object method, you do it using the self keyword.0662

When we are outside, we use the Person keyword--the name of the class.0674

You actually can do that within the class, but typically, what you do is use the self keyword,0677

because self means, "The class I am referring to is the same class of this object instance," which would be a Person class.0682

"And I want to get the description property."0690

So, what we have here is: we go ahead and include this new Person class definition and create two new people, Joe Smith and Jane Jones.0693

And then, what we do is: we are calling the getDescription instance method.0704

It is an object method; we are going to learn about static methods in a little bit, but for now, it's just a regular method like we have been using.0710

And what that is going to do is: as we saw, it is going to return the value of the static variable description.0716

So here, we are outputting the description static property for person1 and person2.0722

What we will see is that they are the same property.0730

And then, what we are going to do is change the property, using the syntax for accessing a static variable outside of a class,0732

where you use the class name and the scope resolution operator.0740

We are going to set the static variable description for Person equal to this new description: "No description available."0743

Then, what we are going to do is call this getDescription method on each of these Person objects.0749

And what we are going to see is that it is updated for both of them.0754

And so, in this example, we learned about how to access a static property from within a class method, or an object method.0757

And then, it also demonstrates how it is that one static property is for all of the different instances of a class.0765

When we call getDescription on this person1 and person2 object, and it returns that static variable,0772

they are both going to have that same value that they returned it to.0779

You can't have a separate value for the static property description for each instance of a class.0781

It is the same for all the classes of that type.0791

So, if we go and look at the script in our browser, insideClass.php, we can see "Create two Person instances," and there is some output from our constructor.0794

We created a Person Joe Smith, and Jane Jones.0808

And then, we can see that, when we first try to output the static property, which is before we changed it0811

(it was immediately after we created these Person objects, so for example, we instantiate the Person objects,0817

and then we immediately output the static variable description and the static default value for that variable,0823

"This class represents a person), it outputs "This class represents a person."0830

Now, when we go back and look at insideClass after we have changed it to "No description available,"0836

when we go back and run that same getDescription method that is going to return the value of the static variable description,0840

both objects are going to return that same string, "No description available."0847

And we can see, the second time we call it: they both output "No description available."0853

Somewhat of a more useful example of using an instance property is an instance counter, sometimes called a reference counter.0860

It is keeping track of the number of instances that you have of a particular class.0868

For example, each time you create a Person object, if you have a Person constructor, that constructor is going to get called.0873

What you can do is increment this; for example, here we have a static variable for the Person class called numPeople.0880

And this variable is the same for all instances of the Person class.0887

So, every time in our constructor, what we do is increment it by 1.0892

Every time we create a new Person object, we are incrementing this static variable numPeople by 1.0897

So, any time outside the class that we reference this static variable Person::numPeople, and we see what this value is,0901

it is going to equal the number of times we have run this constructor.0909

And it is going to be the same across all different classes, so when we increment numPeople here,0912

it doesn't just do it for any particular instance of this class; it does it for all of the Person classes.0916

We just changed our constructor a little bit to increment this counter variable called numPeople.0924

And then, the other thing that we can do is that we can create a destructor that, when the destructor is run--0931

when the object is destroyed--it decrements the numPeople static variable.0937

So, every time we create a Person object, the counter is going to go up by 1.0941

Every time we get rid of it, the counter is going to go down by 1.0944

We have a script here where we defined three different instances of a Person object that all have their own firstName and lastName instance properties.0948

But they all share the same numPeople static property.0961

And then, we have a statement here that just outputs, after we have created all these, a statement that says,0966

"The current number of Person instances is" and then we access that static variable Person, numPeople.0971

In this case, it is going to say 3, because we have created 3 Person objects.0978

The constructor is called 3 times, and for that case, it has been incremented 3 times.0982

Then, what we are going to do is get rid of one of the Person objects; we are going to set it equal to null.0988

That is going to implicitly call the destructor, which is going to decrement it by 1.0992

So now, it is going to say "The current number of Person instances" equals 2, when we again try to reference Person, numPeople.0996

If we go and look at this script in the browser, we can see an outline of what happens.1003

Before we even created any of the objects, I had an output statement that output that static variable for Person, numPeople.1014

Even for a class, even if you haven't instantiated an object of the class Person, you can still access its static properties,1023

because they are specific to the class, not to a particular instance.1030

If we go back and look at the code, I missed mentioning this line here, where before we have even created any Person objects,1035

we are still able to access this numPeople static property of the Person class,1043

because it is on a class level, rather than an instance level.1050

And if we can see, we had initialized it to 0; so when we first run this output, it is going to say "The current number of Person instances is 0."1054

Then, we say we are creating three Person instances, and it shows us the three people we created.1064

When we output, now, the value of that static property numPeople, it is going to say 3.1069

We destroy a Person instance--we get rid of the first one, Joe Smith.1075

And then, when we output the static value again, numPeople, we can say the current number of instances is 2.1079

And so, it shows that it was able to decrement.1085

And down here, you can see: these destroying messages come from our destructor.1087

And the reason they are down here is because we know, when a script ends, PHP destroys all of the objects,1092

or calls a destructor on any objects that were still being used.1098

And so, since these objects were in use, their destructors get called automatically.1102

So, even though we didn't specifically call them, that is why they show up here at the bottom.1106

And so, that is somewhat of a more practical example of how you might use a static variable 1110

to keep track of, for example, the number of instances of a particular class.1116

And the reason you can do that is because it is the same for all of the different instances,1121

and when each one updates that static variable, all of the other ones know about it.1125

Now, we are going to talk about something called static methods, which I hinted at a little bit.1132

And it is essentially the analog of...where static properties are to instance properties, static methods are to instance methods.1136

And instance methods we haven't talked about, but what we have been declaring so far and defining in our class definitions have been instance methods.1145

They are methods that you call using an instance of an object.1153

For example, if we have an object a, and we wanted to say get name, this is an example of an instance method.1157

It works on an instance of a particular object.1171

It is going to have access to the instance properties of this particular instance, whatever class it may be.1174

And you use the arrow operator to get access to it, because it is specific to an instance of a class.1181

A static method, on the other hand, is not specific to an instance of a class.1188

And we are going to see that you access it using the scope resolution operator, as well.1193

The way you declare a static method is just like you do any other method in a class, except you include the static keyword.1199

And because static methods are not associated with a particular instance of a class, they can't access the $this variable within their definitions.1207

Now, this should make sense, because if you remember, the $this variable, when we use it within an object's...1218

what I want to refer to as instance methods, $this refers to this particular instance of an object.1226

Because a static method doesn't relate to an instance, it doesn't know about this.1233

It doesn't know about any particular object; it just knows about the class definition as a whole.1239

So, you can't access this within static methods, or you will get an error.1244

And as mentioned, the non-static methods, which is what we have been using up until now, are called instance methods.1249

And static methods are called using the scope resolution operator, just as we did with the static properties; we are going to see that in a second.1255

Outside of the class, you preface it with the class name.1264

For example, if getName was a static public method, we would call it like this from outside the class.1266

Within a class method, we would call it using the self keyword.1280

And again, note that we are using this scope resolution operator, as opposed to the arrow operator,1292

because we are not accessing a particular instance of a class.1297

Let's take a look at...I created this class called Integer that is sort of an object that wraps an integer value.1301

It has one private instance property that is called intVal, and it just represents an integer.1310

So, when we call our constructor, we pass it an integer (1, 2, 3, whatever), and then our constructor sets intVal equal to that integer.1319

We have an object method, or an instance method, called add, in which we can pass it an integer as a parameter.1327

And what it is going to do is access whatever particular instance we are calling it on (this instance)--1337

it is going to access its current int value, add it to whatever you pass in, and then return it.1344

If we look at our code, here we are creating an Integer instance, just as we do normally, using the new operator.1351

We are passing it that we want to set that class equal to (in this case, 1).1365

And then, we are adding 1 to it; and we are saying, "The sum of 1 and an integer with the value 1 equals..."1370

and then we are calling the addInstance method and passing it 1.1376

So, we are saying, "Add 1 to our current integer," which is already 1, so it is going to output 2.1380

Here, you can see: because this is an instance method, it uses this arrow (or dash, greater than) operator to access that instance method.1385

And it does so on a variable that has been set to a new instance, or to an instance of the Integer class.1395

Now, on the other hand, we could define a static method in this class--a static keyword called integers.1403

We simply pass it two integers, and it just returns the addition of them.1410

So, it does the function for us; but we don't actually need to access any object properties.1414

We don't need to access any instance properties, whereas up here, because this was an object method1420

and we wanted to add something, we accessed what the current integer value 1425

of this Integer object was, and then added to the value that was passed in.1433

Now, this is like a stand-alone function, but within the context of a class.1437

So now, if you want to add 1+1, instead we could access that static method addIntegers, and pass it the parameters 1 and 1.1442

And the way we call it is by prefacing it with the name of the class and the scope resolution operator.1451

This is saying, "Call the addIntegers static method defined in the Integer class, and pass it the parameters 1 and 1."1459

And it is going to return the addition of those.1467

So, when we view our script, we are going to see the sum of 1 and our Integer object that has int value equals 2.1468

The Integer value equal to 1 is going to equal 2.1483

And then, when we call the static method, the sum of 1 and 1 equals 2.1485

So, in this case, we haven't acted on an instance.1489

Now, one thing to note is that I mentioned that, just like with static properties, you can access static methods within an object, as well.1492

And so, in this case, we actually could do this.1504

If we wanted to, we could call this addIntegers method from within our add function, and have that do all the work.1508

It doesn't really make sense, but this is just an example to show how you would access a static method within another class method.1517

Use the self keyword, followed by the scope resolution operator, then the name of the method.1525

And then, in this case, we would pass it 2 values, because it is a function that takes 2 integers.1533

In this case, we are going to pass it the integer value of this particular instance of the object.1538

And then, we are going to pass it (to add to it) the integer that we pass in to this add method.1544

Now, when we go ahead and run our script, we are going to see that the same thing is output,1552

because if we look back here, when we call int1-->add(1), what we are doing is saying, "Call the add instance method on this instance of the integer."1555

It is going to go to our Integer class; it is going to call this add method, which then calls this static method addIntegers.1564

And it gives it the value and says, "Add the current value of this Integer object, plus the value that we passed in."1571

And when we refresh our script, it is going to show the same thing.1577

That is a way to also access a static method within another object method.1581

Just to try and sum things up in maybe a more visual fashion, we have this class/object diagram that we looked at a little bit when we first introduced objects.1591

Let's say we have this Person; this is a class that we defined in a class definition file somewhere.1604

And this is shorthand; this isn't correct syntax; but let's say we have defined three properties for this Person class.1608

firstName and lastName are both instance properties, so they are particular to each instance of a class.1615

And then, we are defining a static property, called desc, which is short for description.1622

And so, that is going to be a property that is the same for all of the different instances of a Person class.1627

Then, we are going to define two methods: we are going to define an instance method called getFullName,1633

which is going to operate on a particular instance of a class.1638

And it is going to have access to the instance properties of a particular class.1641

We are also going to define a static method called getDesc, which is just going to return the value of this static variable description.1645

And so, this is a static method.1655

When we create...for example, if we use the new keyword, Person, every time we did that, we would be creating an instance of the Person class.1657

This is an instance of the Person class; in this case, maybe we would assign it to the variable called person1.1669

So now, we have an object, and an object is an instance of this class.1674

And an object gets its own copies of instance properties.1678

We are going to assign Joe, for example, to the firstName instance property, Smith to the lastName instance property.1682

In addition, it gets its own copy, in a way, of getFullName.1689

So, when you call getFullName on this instance (maybe you would call it like this...and maybe it would return the concatenation1693

of firstName and lastName), this method is an instance method that has access to these instance variables.1702

Essentially, what I have shown is that here, we have just created two objects, person1 and person2.1709

You can see that the properties that are instance properties and the instance methods 1714

get their own copy made in each instance of an object that is created.1720

However, this static method stays up here; and so, when you access a static method or a static property,1725

it is almost like you are calling them on the class itself.1734

Whereas down here you would access the instance properties or instance methods using the arrow operator; 1737

now we have this new scope resolution operator to access static properties--in this case, description.1743

And that is how we would access that.1753

It is kind of like all of the things that are static stay in the class.1756

It is almost like a class, in a way, is an object of itself; it has its own properties, and it has its own methods.1760

In this case, it has its own description method that is specific just to that Person class.1766

It has its own getDescription method, which is specific to that Person class that anybody can call (assuming they are public methods).1771

But it doesn't have access to these instance properties of these instance methods.1778

Those are particular to each new object that is created.1783

Again, this is kind of a tricky concept, which is why I am focusing on it a lot.1788

It is just that when you create an instance of a class, any non-static properties or non-static methods1795

(which are instance properties and instance methods) get their own copy created for that particular instance.1803

The static properties and the static methods don't get copied; they have one copy1809

that resides in this global class definition that all of the objects have access to.1814

And it is shared between all of the instances of the objects.1820

One other related topic is class constants.1826

We know how to define constants within our regular scripts.1829

Well, you can define a constant within a class that is specific to that class.1832

You do it with a const keyword, and then you define the constant as you normally would.1835

Here, it is just alphanumeric characters--the same rules for naming a variable, except there is no dollar sign.1842

And the convention is still to capitalize all of the letters of a constant's name.1850

And then, you provide it a value.1855

So here, we are declaring a Math class, and we are declaring a constant in this Math class called PI, 1857

and setting it equal to the approximate value of pi, 3.14.1863

The way you access a class constant: because it is a constant, it can't change, so it is going to be the same in all the different classes.1867

So, it makes sense that you would access it kind of like a static property.1874

And you do that outside the class using the scope resolution operator again.1877

So, you have the class name (in this case, Math), and the scope resolution operator, followed by the name of the constant.1883

That is how you would access that from outside the class.1890

Inside the class, you would use the self keyword.1892

And then again, notice: the one thing here is that there is no dollar sign.1895

If PI were a static property, you would access it maybe like that after the scope resolution operator.1899

That is one key thing to note between static class properties and static class constants: the absence of the dollar sign.1907

Let's take a look at an example of that.1916

I created a class called Circle; we defined a constant in it, PI.1919

PI equals 3.14; and then, we defined a static function, which is a function that is just particular to the Circle class.1924

Anybody that knows about the Circle class can access this public static function.1932

And what it does is calculates the area of a circle.1939

You pass it the radius, and the formula for the area of a circle is pi, r squared (pi times the radius times the radius).1941

What this function does is accesses this class constant PI using the self keyword in the scope resolution operator.1949

And then, it says to multiply it times the radius twice.1957

You can call this function in a stand-alone way, and you call it the same way that we learned about accessing static methods.1961

And you would call it by using the name of the class, Circle, the scope resolution operator, 1969

the name of the static method that we want to call, and then also the parameter that we are passing.1973

We are saying we want the area of a circle with radius 2.1980

This is an example of calling this area static method on a Circle class that makes use of this constant PI.1983

And then, up here, we just have an output statement that shows how we access the class constant PI outside of the class.1991

And we use Circle (the class name), the scope resolution operator, and then the name of the constant.1998

And that, again, is different from when we access it within a class method, because here we use the self keyword.2005

So, if we were to go ahead and load this, we can see that it outputs the value of PI as 3.14.2013

So, we were able to access that class constant, using the scope resolution operator in the class name Circle.2022

And then, we were also to calculate the area of a circle with radius 2 by accessing that static method called area,2028

using the Circle class name, the scope resolution operator, and area.2034

For the homework challenge, what I would like you to do is build upon the Circle class that we just talked about from lecture example version 5.2043

And what I would like you to do is add an instance property to this class, called radius, that is going to represent the radius of a circle.2050

And add a public static property called numCircles, and we are going to use that as an instance counter.2057

What we are going to have you do is create a constructor that takes a radius as its parameter.2065

And what it is going to do: it is going to set that radius instance property that you are going to be declaring equal to whatever value you pass in.2069

And then, also, in the constructor, it is going to increment the static property numCircles by 1.2079

So, that is going to keep that reference count, because that numCircles is going to be for all of the Circle classes.2085

I also want you to create a destructor that, when it is called, decrements numCircles by 1.2091

And so, every time a object is created, that static variable numCircles is going to be increased by 1.2096

Every time an object is destructed, it is going to be decreased by 1.2101

Then, just output the value of the constant PI, which is already defined in the class that we saw.2105

And that will just give you practice accessing a class constant using the scope resolution operator.2110

Use the static area method, just like we had in our example, to output a circle with the radius of 2.2117

And then, I want you to create an instance of the Circle class.2127

Pass the constructor a value 2; so what it is going to do--the Circle constructor should take the value 2 and set its instance property radius equal to 2.2131

And then, create a calcArea instance method, which is going to calculate the area of a particular instance of a Circle object.2144

This is going to be an instance method, not a static method.2157

It is going to have access to the $this variable, so you will be able to access the radius using this, followed by the arrow, followed by 'radius.'2162

Use that, and within your calcArea method, you will be able to grab the value of the radius property 2173

of that particular instance, calculate the area, and return it.2182

And when you call it--let's say you defined a circle variable and you call it circle1--you would be calling this method using the arrow operator.2186

And note that we don't have to pass it the value of 2, because the circle object already has a radius property defined 2199

that already knows about it; and that is how you access it with this.2206

Repeat steps 6 and 7 for another Circle; so basically create two Circle objects.2211

Then, output the value of the static property numCircles after you have created these two, and you should verify that the value is 2.2216

That shows that you are properly incrementing that static variable that is keeping track of our instances.2222

Then, destroy one of the Circle objects by just setting it equal to null.2228

And then, re-output the numCircles property and verify that it has been decremented to the value of 1.2231

And so, that will give you practice with accessing the static variable outside of a class, so you would be accessing numCircles.2237

This homework example will give you practice accessing class constants, class static variables or properties, and class static methods.2254

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