메뉴 건너뛰기

프로그램언어

조회 수 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 버전 숨기기 ( php version hide )

    Date2024.02.07 Views71
    Read More
  2. php 두날짜 사이의 모든날짜 배열 만들기

    Date2023.01.12 Views178
    Read More
  3. PHP str_replace php 문자열치환

    Date2023.01.12 Views197
    Read More
  4. PHP 이미지 리사이즈 함수 imagecopyresized

    Date2023.01.12 Views212
    Read More
  5. 코드 생성 하기

    Date2023.01.12 Views213
    Read More
  6. HTTP 인증하기, 로그인창 띄우기

    Date2021.03.26 Views214
    Read More
  7. [PHP기초] 상속기본

    Date2021.03.27 Views214
    Read More
  8. 폴더 용량 체크

    Date2023.01.12 Views220
    Read More
  9. PHP SimpleHtmlDom Parser로 HTML 파싱하기

    Date2023.01.12 Views220
    Read More
  10. 웹페이지 파싱

    Date2023.01.12 Views226
    Read More
  11. 조건문의 함수실행 여부 if ( 0 && ... )

    Date2021.03.26 Views227
    Read More
  12. ereg(), eregi(), ereg_replace(), eregi_replace(), split() 대체

    Date2023.01.12 Views227
    Read More
  13. PHP 파일 업로드 FORM 처리

    Date2023.01.12 Views229
    Read More
  14. 배열 연산자 []= 에 대해서

    Date2021.03.26 Views231
    Read More
  15. 클래스와 인스턴스 그리고 메소드 만들기

    Date2021.03.27 Views231
    Read More
  16. PHP 디렉토리안에 파일 리스트 가져오기

    Date2023.01.12 Views233
    Read More
  17. php www 붙이기

    Date2023.01.12 Views234
    Read More
  18. 상수, 마법상수, 모든 상수 보기

    Date2021.03.26 Views236
    Read More
  19. PHP http -> https 로 전환

    Date2023.01.12 Views236
    Read More
  20. PHP 하위 디렉토리 포함 디렉토리 리스트 출력

    Date2023.01.12 Views236
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved