How can data be stored in class variables in PHP?

To store data in class variables in PHP, you can declare class properties and assign values to them within the class. These class properties will then hold the data for the class instance. You can access and modify these class variables using object instances of the class.

class MyClass {
    public $data;

    public function __construct($data) {
        $this->data = $data;
    }

    public function getData() {
        return $this->data;
    }

    public function setData($newData) {
        $this->data = $newData;
    }
}

// Create an instance of the class
$myObject = new MyClass("Hello, World!");

// Access the class variable
echo $myObject->getData();

// Modify the class variable
$myObject->setData("Goodbye, World!");

// Access the modified class variable
echo $myObject->getData();