Menu
Coddy logo textTech

Properties

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 6 of 91.

Properties are variables that belong to a class. They store information about the object's state. In PHP, properties must be declared inside the class using a visibility keyword like public.

Properties can have default values that are shared across all objects:

<?php
class Student {
    public $school = "PHP Academy";  // default value
    public $name;
    public $age;
}

Properties without a default value start as null until you set them.

You can set property values using a method and $this:

class Student {
    public $school = "PHP Academy";
    public $name;
    public $age;

    public function setInfo($name, $age) {
        $this->name = $name;    // set instance data
        $this->age = $age;      // set instance data
    }
}

Create student objects and set their individual data:

$student1 = new Student();
$student2 = new Student();

$student1->setInfo("Alice", 20);
$student2->setInfo("Bob", 22);

Access the properties:

echo $student1->name . "\n";          // Alice
echo $student2->name . "\n";          // Bob
echo $student1->school . "\n";        // PHP Academy
echo $student2->school . "\n";        // PHP Academy

Output:

Alice
Bob
PHP Academy
PHP Academy

Key Difference: Properties with default values (like $school) start with the same value for every object. Properties set through methods (like $name and $age) are unique to each object.

challenge icon

Challenge

Easy

Complete the Student class in student.php:

  1. Declare a property $school with default value "PHP Academy"
  2. Declare properties $name and $age (no default values)
  3. Add a setInfo method that takes $name and $age parameters and sets them using $this

The driver.php file will test your class by creating two students and printing their information.

Cheat sheet

Properties are variables that belong to a class and store information about the object's state. They must be declared inside the class using a visibility keyword like public.

Properties can have default values shared across all objects:

<?php
class Student {
    public $school = "PHP Academy";  // default value
    public $name;
    public $age;
}

Properties without a default value start as null until set.

Use methods and $this to set property values:

class Student {
    public $school = "PHP Academy";
    public $name;
    public $age;

    public function setInfo($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
}

Create objects and set individual data:

$student1 = new Student();
$student1->setInfo("Alice", 20);

echo $student1->name;    // Alice
echo $student1->school;  // PHP Academy

Properties with default values start the same for every object. Properties set through methods are unique to each object.

Try it yourself

<?php
require_once 'student.php';

$student1 = new Student();
$student2 = new Student();

$student1->setInfo("Alice", 20);
$student2->setInfo("Bob", 22);

echo $student1->name . " is " . $student1->age . " at " . $student1->school . "\n";
echo $student2->name . " is " . $student2->age . " at " . $student2->school . "\n";
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming