What is the significance of encapsulation in PHP classes?

Encapsulation in PHP classes is significant because it allows for the bundling of data (attributes) and methods (functions) that operate on that data into a single unit. This helps to keep the data safe from outside interference and misuse, as access to the data is restricted to only the methods within the class. Encapsulation also promotes code reusability, maintainability, and helps in achieving the principle of data hiding.

<?php
class User {
    private $username;
    private $password;

    public function setUsername($username) {
        $this->username = $username;
    }

    public function getUsername() {
        return $this->username;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function getPassword() {
        return $this->password;
    }
}

$user = new User();
$user->setUsername("john_doe");
$user->setPassword("password123");

echo $user->getUsername(); // Output: john_doe
echo $user->getPassword(); // Output: password123
?>