메뉴 건너뛰기

프로그램언어

조회 수 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. [PHP기초] 접근제어자(access modifier)

    Date2021.03.27 Views288
    Read More
  2. 그누보드 https 보안서버 연결시 오류

    Date2023.01.12 Views287
    Read More
  3. MYSQL DB 다중접속을 해결 하는 한 방법

    Date2021.03.26 Views284
    Read More
  4. Predefined Variables (미리 정의된 변수들)

    Date2021.03.26 Views274
    Read More
  5. printf() sprintf()

    Date2021.03.26 Views272
    Read More
  6. 가변변수로 만든 배열

    Date2021.03.26 Views272
    Read More
  7. 큰따옴표(") 와 작은따옴표(')

    Date2021.03.25 Views271
    Read More
  8. PHP - 공공 DATA XML 파싱(PHP 버전)

    Date2023.01.12 Views270
    Read More
  9. [PHP 기초] 함수에 관해서

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

    Date2021.03.27 Views265
    Read More
  11. 간단한 캡차파일 만들기 captcha

    Date2023.01.12 Views264
    Read More
  12. [PHP기초] 데이터 집합 - 배열다루기

    Date2021.03.27 Views263
    Read More
  13. php 이미지 리사이징 image resizing

    Date2023.01.12 Views259
    Read More
  14. 구글 학술 검색

    Date2021.03.26 Views255
    Read More
  15. PHP 브라우저 알아내기

    Date2023.01.12 Views253
    Read More
  16. current() next() key() 그외 배열관련 함수

    Date2021.03.26 Views253
    Read More
  17. PHP ZIP 압축파일 만들기

    Date2023.01.12 Views251
    Read More
  18. 파일 output을 return 하기

    Date2021.03.26 Views243
    Read More
  19. php 간단 심플한 달력만들기

    Date2023.01.12 Views241
    Read More
  20. date() 함수의 출력 형식

    Date2021.03.26 Views241
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved