In addition to properties, we can define class methods – essentially functions each object will contain. Methods are frequently used to interact with an object’s properties in a defined manner.

Methods are defined with the same syntax we use when declaring functions (except they are defined within the curly brackets of a class).

Given a Pet class with first and last name properties, we could provide a method which returns the two properties combined into a full name:

class Pet { public $first, $last; function getFullName() { return $this->first . " " . $this->last; } }

The $this variable refers to the current object; when we invoke this method, $this refers to the specific object that called the method.

Methods are accessed in a similar fashion to properties, using the object operator (->), but in order to invoke them, use parentheses at the end:


So, to access the full name of our Pet, we can use the following:

$very_good_groundhog = new Pet(); $very_good_groundhog->first = "Punxsutawney"; $very_good_groundhog->last = "Phil"; echo $very_good_groundhog->getFullName(); # Prints "Punxsutawney Phil"



Add a getInfo method to our Beverage class. We’ll be using this method to return some information about our objects.


The method should return this statement about the beverage, with <temperature> and <color> replaced with the beverage’s temperature and color:

"This beverage is <temperature> and <color>."

We’ve created an instance of Beverage and saved it in the variable $soda. We’ve also assigned some values to its properties.

After our code, print the result of calling getInfo on the object (and nothing else).

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?