반응형
<?php
//반환값이 없는 Function Example1
function meal(){
echo("soup");
}
meal();
//반환값이 있는 Function Example2
function travel($country, $lang){
return $country ."의 언어는" . $lang ."입니다";
}
echo travel("South Korea", "Korean");
class Student{
public $name;
public $age;
public function __construct($name, $age){
$this->name =$name ;
$this->age =$age;
}
public function Record(){
$string = "학생의 이름은" . $this->name . "학생의 나이는" . $this->age;
return $string;
}
}
$student1 = new Student("James", 10);
echo $student1 -> Record();
?>
Student이라는 클래스를 선언해주었다.
Student이라는 클래스내에서 항상 사용가능하기위해서 public으로변수 $name, $age를 선언해주었다.
생성자함수는 Student클래스가 불려지자마자 제일 먼저 실행이 되는 함수이다. php에서는 __construct로 선언해주어야한다. new라는 키워드를 사용해서 Student클래스의 인스턴스를 생성한다. james와 10을 넘겨주면 생성자함수가 먼저 실행이 되어 $this->name에는 james, $this->age에는 10이 들어가 있다.
반응형
'BackEND > PHP' 카테고리의 다른 글
json_encode() : PHP의 데이터 구조를 JSON 문자열로 변환 (0) | 2023.07.01 |
---|---|
PHP기본-세션에 대하여 (0) | 2023.05.28 |
PHP기본 - 쿠키에 대하여 (0) | 2023.05.28 |
PHP기본-Syntax모음 (0) | 2023.05.22 |
VSCODE extension설치 (0) | 2023.05.17 |