VIDEO
접근 제어자 1 - 개념
접근제어자(access modifier) = 속성의 가시성(property visibility)
캡슐화 달성 수단 중 하나.
캡슐화(encapsulation)
객체 사용자에게 필요한 정보/기능만 노출시켜서, 편의성 및 안정성 높이는 작업.
VIDEO
접근 제어자 2 (private vs public)
<?php
class MyFileObject{
private $filename;
function __construct($fname){
$this->filename = $fname;
if(!file_exists($this->filename)){
die('There is no file '.$this->filename);
}
}
function isFile(){
return is_file($this->filename);
}
}
$file = new MyFileObject('data.txt');
// $file = new MyFileObject();
// $file->filename = 'data.txt';
var_dump($file->isFile());
var_dump($file->filename);
?>
VIDEO
접근 제어자 3 - (private vs public)
<?php
class Person{
private $name;
function sayHi(){
print("Hi, I'm {$this->name}.");
}
function setName($_name){
if(empty($_name)){
die('I need name');
}
$this->name = $_name;
}
function getName(){
return $this->name;
}
}
$egoing = new Person();
$egoing->setName('egoing');
$egoing->sayHi();
print($egoing->getName());
?>
접근 제어자 3 (method visibility)
<?php
class Person{
private $name;
public function sayHi(){
print("Hi, I'm {$this->name}.");
}
public function setName($_name){
$this->ifEmptyDie ($_name);
$this->name = $_name;
}
public function getName(){
return $this->name;
}
private function ifEmptyDie ($value){
if(empty($value)){
die('I need name');
}
}
}
$egoing = new Person();
$egoing->setName('egoing');
$egoing->sayHi();
print($egoing->getName());
?>
https://opentutorials.org/module/6/15730
PS.
Visibility
https://www.php.net/manual/en/language.oop5.visibility.php
접근제어자
https://homzzang.com/b/php-1065
주소 복사
랜덤 이동
최신댓글