Class properties
Objects typically represent state (data) and behavior (functionality).
To represent data, classes use properties: variables that are part of the class definition.
Properties must have at least two things: a name, and an access modifier that specifies the property's visibility: public, private, or protected:
For now it's perfectly fine to keep everything public. You'll learn more about access modifiers later.
Let's create a Phone instance using the new keyword, and set the brand to "Samsung":
Pay attention to the last line. The object operator (->) is used to access the "brand" property on the $myphone object. Notice that there's no $ sign between the arrow and the property name: $object->property_name.
You can now print your phone's brand:
Note
You will learn later that it is NOT a good practice to mix the class definition with code that uses that class in the same file.
A class should go into its own file, and that class file should be referenced using one of methods PHP provides for this.
For now it's fine to use a single file to keep things as straightforward as possible.
Do not skip the next activity, which shows you how to run a PHP script from the command-line.
Activity 1
- Recreate the PHP script that sets and prints the phone brand.
- Save it as myphone.php.
- run it with
php -f myphone.php
You can either run this from your terminal application or from a terminal inside VSCode.
If everything went well, the brand name should have been printed.
Activity 2
It might be a bit hard to read the output of the previous script since it sticks to your command-line prompt.
A quick fix is to add a newline before and after the value you want to print on screen. There are several ways to do this; using the PHP_EOL (End Of Line) reserved constant ensures that the correct line ending characters are used on your system.
Multiple properties
Of course classes can have more than one property. You can add as many as you need:
Activity 3
- Instantiate a Phone object using the class definition from Activity 1.
- Do not provide any values for brand, model, color, or price.
- What do you think will be printed when you try to
print $phone->price? - Why?
Activity 4
- Instantiate a Phone object.
- What do you think will be printed when you try to print a property that doesn't exist, such as
$phone->size?
Class methods
Methods are functions that are part of a class definition - just like properties are variables or constants that are part of a class definition.
Methods need the same access modifiers that properties do: public, private, or protected. If you don't specify one, public will be assumed. Learn more in the section on access modifiers.
Example without return value
Let's update the Phone class and add a method to call another phone number:
Example with return value
As you can see, a method really behaves like any other function.
Variable scope
The scope of variables is limited to the method in which they are declared. In other words, variables declared in one method are not visible in other methods nor outside the class.
The same goes for class properties: a class method cannot directly access any of its properties. For a method to access its properties, you need to use the $this keyword.
The $this keyword
You have learned that you can invoke an object's method or access an object's property by using the object operator (->) on that object:
But what do you do when you want to write a method that uses another method of the same class, or accesses a property of the same class?
Example: a method that prints the phone's brand name.
You could TRY the following, but it would be WRONG:
In the above example, the statement print $brand will result in a warning or error because $brand refers to a variable named "brand" (which does not exist), not a property named "brand" (which does exist).
This is why the $this keyword exists: it's a pseudo-variable that means "the current object".
Here's how you use $this:
What you're telling PHP is that whenever any Phone object is instantiated and someone invokes the printBrand() method on that object, the method will take that object's $brand value and print it.
Activity 5
- What do you think will be printed in this example?
Why?
class Phone {
public $brand
public function printBrand() {
$brand = 'Apple';
print $this->brand;
}
}
$myphone = new Phone();
$myphone->brand = 'Samsung';
$myphone->printBrand();
You've seen an example of accessing a property from within a class. Now let's look at how one method invokes another method inside a class:
Explanation
When invoked, printBatteryLife() invokes calculateBatteryLife() to get the actual battery life, and then prints that value.
We certainly could have kept all this code in a single method, but it's a good practice to create several smaller methods that each have their own responsibility and do one thing, rather than mixing it all together in one long method.
Class constants
Constants are similar to variables: you can assign a value to them, you can print them, and can pass them to functions or methods as parameters.
However: constants are immutable: once you've assigned a value to a constant, their value can no longer change while the code is being executed.
Attempting to assign a value to a constant that is already defined will result in an error.
Note:
- Constant names are not prefixed with a
$. - Constant names always use UPPER CASE notation by convention
Basic const example
Class const example
Class properties are typically variables, but you can use consts too:
$this vs self::
Accessing constant properties uses different syntax than addressing variable properties:
→ When you want to access a class const, you need to use the self keyword in combination with the so-called "double colon" scope resolution operator: self::NAME_OF_CONST
Summary
- You can use a class definition to instantiate objects of that class / type.
- New objects are instantiated using the
newkeyword. - Objects typically represent state (data) and behavior (functionality).
- Class properties contain data; class methods provide functionality.
- The
$thispseudo-variable andselfkeyword reference the current object. - Use the
::scope resolution operator to access class consts.