<?php
class ClassName {
private $__username;
private $__password;
function setUsername($value) {
$this->__username = $value;
}
function setPassword($value) {
$this->__password = $value;
}
function getUsername() {
return $this->__username;
}
function getPassword() {
return $this->__password;
}
}
$class = new ClassName();
$class->setUsername('Lionel Phạm');
$class->setPassword('12345678');
echo $class->getUsername();
echo $class->getPassword();
C:\xampp\htdocs\php\demo.php
<?php
class ClassName {
public $username = 'ab';
function __set($key, $value) {
if ($key = 'somekey') {
$this->username = $value;
}
}
function __get($key) {
if ($key == 'somekey') {
return $this->username;
}
}
}
// $class = new ClassName();
// $class->somekey = 'Lionel Phạm';
// echo $class->username; // kết quả là Lionel Phạm
$class = new ClassName();
echo $class->somekey; // kết quả là ab