메뉴 건너뛰기

프로그램언어

조회 수 288 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

static.php

<?php
//클래스 변수
//고정된 이라는뜻
// static : 모든 인스턴스가 공유하는 변수 만들기!!
//static 변수는 class 명으로 바로 접근할 수 있다.(instance 즉 객체를 만들지 않고 접근가능하다.)

class Person{
 
private $name; //다른 객체가 접근할 수 없다. 즉 공통으로 사용할 수 없다.
 
private $count = 0;
 
//생성자: 객체가 생성될때 필요한 값을 셋팅한다.
function __construct($name){
$this->name = $name;
$this->count = $this->count +1;
}
 
function enter(){
echo "<h1>Enter ".$this->name." {$this->count} th</h1><br>";
}
 
}
 
$p1 = new Person('abcnt');
$p1 ->enter();

$p2 = new Person('하하하');
$p2 ->enter();
 
$p1 = new Person('호호호');
$p1 ->enter();

?>

 

static2.php

<?php
//클래스 변수
//고정된 이라는뜻
// static : 모든 인스턴스가 공유하는 변수 만들기!!
//static 변수는 class 명으로 바로 접근할 수 있다.(instance 즉 객체를 만들지 않고 접근가능하다.)

class Person{

//인스턴스 변수
private $name; //다른 객체가 접근할 수 없다. 즉 공통으로 사용할 수 없다.

//클래스 변수 // 모든 인스턴스가 공유한다!!
//클래스 변수에는 self::를 사용한다.
private static $count = 0;
 
//생성자: 객체가 생성될때 필요한 값을 셋팅한다.
function __construct($name){
$this->name = $name;
self::$count = self::$count +1;
}
 
function enter(){
echo "<h1>Enter ".$this->name." ".self::$count." th</h1><br>";
}
 
//클래스 이름으로 호출되려면 static을 호출해야 한다.
static function getCount(){
 
//클래스 변수에는 self를 사용한다.
return self::$count;
}
 
}

$p1 = new Person('abcnt');
$p1 ->enter();
 
$p2 = new Person('하하하');
$p2 ->enter();
 
$p1 = new Person('호호호');
$p1 ->enter();
 
//클래스 변수 호출
echo Person::getCount();
 
?>

 


 


  1. 공백문자 체크

    Date2019.01.08 Views1625
    Read More
  2. 경로 제외한 파일 이름만 선택하는 방법, Basename()

    Date2020.11.23 Views429
    Read More
  3. 게시판 페이징 기법과 개념

    Date2017.03.06 Views17323
    Read More
  4. 게시판 내용 숨김 클릭시 내용 출력 [ 참고 ]

    Date2018.07.24 Views4767
    Read More
  5. 검색어 처리 루틴

    Date2015.04.14 Views20778
    Read More
  6. 간단한 캡차파일 만들기 captcha

    Date2023.01.12 Views261
    Read More
  7. 간단한 PHP 파일 업로드, 다운로드 구현

    Date2017.03.06 Views28547
    Read More
  8. 가변변수로 만든 배열

    Date2021.03.26 Views272
    Read More
  9. [이클립스]PHP 개발환경 만들기

    Date2018.07.04 Views7941
    Read More
  10. [PHP기초] 함수와 객체의 비교

    Date2021.03.27 Views291
    Read More
  11. [PHP기초] 접근제어자(access modifier)

    Date2021.03.27 Views288
    Read More
  12. [PHP기초] 생성자(인스턴스 초기화)

    Date2021.03.27 Views265
    Read More
  13. [PHP기초] 상속기본

    Date2021.03.27 Views214
    Read More
  14. [PHP기초] 데이터 집합 - 배열다루기

    Date2021.03.27 Views263
    Read More
  15. [PHP] 한글명 파일 다운로드받기

    Date2014.03.26 Views20551
    Read More
  16. [PHP] 서버 운영하시는분 서버 꺼졌을때 메시지 띄우기

    Date2015.04.06 Views20324
    Read More
  17. [PHP] 게시판 글쓰기와 이미지 파일 DB 저장 및 불러오기 예제

    Date2017.02.19 Views35888
    Read More
  18. [PHP] POST 로 넘어온 모든 변수값 확인하기

    Date2015.03.25 Views23337
    Read More
  19. [PHP 기초] 함수에 관해서

    Date2021.03.27 Views268
    Read More
  20. www가 붙은 도메인과 안붙은 같은 도메인, 로그인 세션 유지

    Date2017.03.07 Views17080
    Read More
Board Pagination Prev 1 ... 3 4 5 6 7 8 9 10 11 12 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved