코드:
결과보기 »
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title>PHP Class</title> </head> <body> <?php class ClassName { public $publicVar; private $privateVar; protected $protectedVar; public function __construct() { $this->publicVar = "public property<br>"; $this->privateVar = "private property<br>"; $this->protectedVar = "protected property<br>"; } public function publicMethod() { echo "public method()<br>"; } private function privateMethod() { echo "private method()<br>"; } protected function protectedMethod() { echo "protected method()<br>"; } } $object = new ClassName(); echo $object->publicVar; // 접근 가능 //echo $object->protectedVar; // 접근 불가능 //echo $object->privateVar; // 접근 불가능 $object->publicMethod(); // 호출 가능 //$object->protectedMethod(); // 호출 불가능 //$object->privateMethod(); // 호출 불가능 ?> </body> </html>