Từ khóa: public, private, protected

PHP5 hõ trợ các thuộc tính public, private và protected.

<?php
class foo {
  public 
$foo=1// ai cũng truy cập được
  
private $bar=2// chỉ được truy cập ở trong phạm vi của lớp
  
protected $baz=3// được truy cập trong phạm vi của lớp và các lớp mở rộng từ nó

  
function p1() {
    echo 
$this->foo $this->bar $this->baz;
  }
}

class 
foo_ex extends foo {
  function 
p2() {
    echo 
$this->foo $this->bar $this->baz;
  }
}

$a = new foo();
echo 
$a->p1(); // will print 123
$b = new foo_ex();
echo 
$b->p2(); // will print 13 + notice about unknown property foo_ex::$bar 
// accessing private/protected properties directly will result is a E_ERROR (fatal error)
echo $a->foo $a->bar $a->baz;
?>

Các thuộc tính PPP cũng có thể được áp dụng cho các phương thức.

<?php
class foo {
  public function 
p() {} // everyone can use
  
private function p2() {} // only class foo may use internally
  
protected function p3() {} // only class foo and it's extender can use
}

class 
foo_ex extends foo 
  function 
foo_ex() { 
    
$this->p2(); 
  } 
}

$a = new foo();
$a->p3(); // fatal error

$b = new foo_ex(); // will fail with fatal error
?>